# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2JsonByYM
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.requestFieldMapDBField import requestFieldMapToDBField_YM
from com.zzh.utils.requestFieldMapDBField import fieldMap_Luo
from com.zzh.utils.jsonUtils_v2.table2Json_Luo import Table2JsonLuo
import random
import time
localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
"""
表名：zzh_Order

说明：会员信息表（包含个人和单位）
"""

#v2 app端的dao层操作
class OrderDaoV2(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def queryOrderById(self, id):
        """
        根据ID查询信息    查询    zzh_user
        :param :
        :return:
        单体查询，按照id查询
        """
        sql = "SELECT * FROM zzh_order WHERE order_id=%s"
        cur = self.conn.cursor()
        total_count = cur.execute(sql, [id])
        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            userOauthJson = Table2JsonByYM.order_list_to_json(result)
            return total_count, userOauthJson
        else:
            cur.close()
            self.conn.close()
            return 0, None

    def queryOrderByOrderNo(self, id):
        """
        根据ID查询信息    查询    zzh_user
        :param :
        :return:
        单体查询，按照id查询
        """
        sql = "SELECT * FROM zzh_order WHERE order_no=%s"
        cur = self.conn.cursor()
        total_count = cur.execute(sql, [id])
        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            userOauthJson = Table2JsonByYM.order_list_to_json(result)
            return total_count, userOauthJson
        else:
            cur.close()
            self.conn.close()
            return 0, None

    def queryOrderByCondition(self, **kwargs):
        """
        条件查询  zzh_user_oauth
        :param :
        :return:
        此处使用userId为条件查询
        """

        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        # 获取对应的映射字典
        fieldMap = fieldMap_Luo.orderAndDepartmentMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM view_orderAndDepartment WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM view_orderAndDepartment WHERE " + whereSentence

        cur = self.conn.cursor()
        total_count = cur.execute(sql)

        if total_count:
            adinfo = cur.fetchall()
            lists = MyPaging().getPage(total_count, pageIndex, pageSize, adinfo)
            adJson = Table2JsonLuo.view_orderAndDept_json(lists)
            return adJson, total_count
        else:
            return None, 0


            # def random_str(randomlength=32):
            #         str = ''
            #         chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
            #         length = len(chars) - 1
            #         random = Random()
            #         for i in range(32):
            #             str += chars[random.randint(0, length)]
            #         return str

    def random_str(self):
        array = ""
        for i in range(0, 31):
            j = random.randint(0, 9)
            array = array + "" + str(j)
        return array

    def insertOrder_Old(self, Info, order_no):
        """
          # 1-4，查询不同的表的时候，做好容错
        'order_no': request.json.get('order_no'),  #前台传递
        'order_ip': request.remote_addr,##确定   读取request
        'order_type': request.json.get('order_type'),  #前台传递     支付类型，1-pc,2-mobile
        'payment_id': request.json.get('payment_id'),  #前台传递     将支付记录与order_id绑定       我该拿这个做什么，传入进来，payment表没有任何符合的数据。
         'user_id': request.json.get('user_id'),  #前台传递
         'coupon_id':request.json.get('coupon_id'),    #couponId  前台传递，用这个id区查询zzh_coupon表的相应记录，做好容错
         'payable_amount': request.json.get('payable_amount'),  #前台传递  应付金额
        'real_amount': request.json.get('real_amount'),  #前台传递  实际支付金额
        'order_amount': request.json.get('order_amount'),  #前台传递  订单总金额

        'trade_no': request.json.get('trade_no'),#???交易号担保支付用到  这个该从哪里取到  先置空

        # 5-9

        'user_name': request.json.get('user_name'),  #根据user_id从zzh_ueser中查询



        'payment_fee': request.json.get('payment_fee'),#？？？支付手续费，先不管，置为空，数据库默认是  0.00


        'payment_status': request.json.get('payment_status',1),#确定   支付状态1默认未支付     2已支付，支付时更新

        # 10-14
        'payment_time': request.json.get('payment_time',''),  #确定    支付时间，创建订单时候默认为空，然后 等到支付的时候再进行调用更新订单表进行更新
        'order_created_time': request.json.get('order_created_time'),  #确定   当前时间，


        'is_invoice': request.json.get('is_invoice',0),  #是否开具发票，是否索要发票 0否，1是
        'invoice_title': request.json.get('invoice_title'),#发票抬头，如果is_invoice为1，根据这个invoice_title和userId添加zzh_invoice记录，同时绑定到invoice_id到order_id
        'invoice_taxes': request.json.get('invoice_taxes'),#？？？这个 发票费用  该从哪里进行读取？

        # 15-19

        'order_point': request.json.get('order_point'),#？？？积分，这个积分将会和uesr_id和point_value积分值一起写入 zzh_point_log  这是收入的积分


        'is_member': request.json.get('is_member'),##确定    是否是VIP，默认0不是 ，1是.   根据user_id查询zzh_member表取出member相关数据，

        # 20-24
        'member_name': request.json.get('member_name'),  #确定
        'member_email': request.json.get('member_email'),  #确定
        'member_telephone': request.json.get('member_telephone'),  #确定
        'member_province': request.json.get('member_province'),#确定
        'member_city': request.json.get('member_city'),#确定

        # 25-29
        'member_district': request.json.get('member_district'),  #确定
        'member_address': request.json.get('member_address'),  #确定


        'is_coupon': request.json.get('is_coupon'),  #？？？优惠券，是否使用优惠券0否，1是  如果使用，则去查询zzh_coupon表？这个地方是不是需要绑定coupon_id？。如果不使用，是否直接置为空
        'coupon_name': request.json.get('coupon_name'),#？？？优惠券名称应该不是唯一的吧
        'coupon_code': request.json.get('coupon_code'),#？？？

        # 30-34
        'coupon_type': request.json.get('coupon_type'),  #？？？
        'coupon_conditions': request.json.get('coupon_conditions'),  #？？？
        'coupon_discount': request.json.get('coupon_discount'),  #？？？



        'service_content': request.json.get('service_content'),#确定   zzh_service表， item里面是有一个service_id字段的 根据item_id对应的service_id去查找这个服务的相关数据
        'service_contract': request.json.get('service_contract'),#确定

        # 35-39
        'service_code': request.json.get('service_code'),  #确定
        'service_name': request.json.get('service_name'),  #确定
        'service_price': request.json.get('service_price'),  #确定
        'service_rate': request.json.get('service_rate'),#确定


        'item_name': request.json.get('item_name'),#确定    根据item_id从zzh_item取出对应数据填充 40-47所有字段

        # 40-44
        'item_content': request.json.get('item_content'),  #确定
        'item_url': request.json.get('item_url'),  #确定
        'item_address': request.json.get('item_address'),  #确定
        'item_publishdate': request.json.get('item_publishdate'),#确定
        'item_deadline': request.json.get('item_deadline'),#确定

        # 45-49
        'item_handlername': request.json.get('item_handlername'),  #确定
        'item_handlerphone': request.json.get('item_handlerphone'),  #确定
        'dept_id': request.json.get('dept_id'),  #确定


        'order_status': request.json.get('order_status',0),#确定    订单状态，默认为0生成订单，之后的状态等到  后续操作的时候更新
        'order_product_is_success': request.json.get('order_product_is_success',0)#确定    产品服务是否成功，默认0未成功,   1表示成功，等服务完成之后进行更新


        """

        # 01-04
        order_message = Info['order_message']
        # order_no = Info['order_no']    #前台传递
        order_ip = Info['order_ip']  # request读取
        order_type = Info['order_type']  # 前台传递
        trade_no = Info['trade_no']  # 前台传递

        user_id = Info['user_id']  # 根据这个读取zzh_user和zzh_member

        payment_id = Info['payment_id']  # 写入支付zzh_payment，绑定order_id和payment_id
        payment_time = " "
        payment_fee = Info['payment_fee']  # 数据库默认为0   ，先置空不管
        item_id = Info['item_id']  # item_id   绑定item
        payment_status = Info['payment_status']  # 默认为1   未支付，    等到实际支付的时候进行更新状态为2
        payable_amount = Info['payable_amount']  # 前台传递
        real_amount = Info['real_amount']  # 前台传递

        order_amount = Info['order_amount']  # 前台传递
        order_created_time = localTime  # 当前时间，订单创建时间
        order_point = Info['order_amount']

        is_coupon = Info['is_coupon']

        coupon_id = Info['coupon_id']  # 前台传递,根据这个读取zzh_coupon的信息
        order_status = Info['order_status']  # 订单状态，默认为0生成订单，之后的状态等到  后续操作的时候更新
        order_product_is_success = Info['order_product_is_success']  # 产品服务是否成功，默认0未成功,   1表示成功，等服务完成之后进行更新
        # 18个
        curUser = self.conn.cursor()
        sqlUser = 'SELECT user_name,user_phone FROM zzh_user WHERE user_id=%s'
        curUserTotalCount = curUser.execute(sqlUser, [user_id])
        if curUserTotalCount:
            curuserNameResult = curUser.fetchone()

            user_name = curuserNameResult[0]  # 获取到用户名
            member_telephone = curuserNameResult[1]
        else:
            user_name = ""
            member_telephone = ""
            errorMessage = "没有符合userId的记录，请检查是否注册"
            return errorMessage
        curUser.close()

        # 20
        curViewMemberAndViewArea = self.conn.cursor()
        sqlViewMemberAndViewArea = 'SELECT * FROM view_member_and_view_area WHERE user_id = %s '
        sqlViewMemberAndViewAreaTotalCount = curViewMemberAndViewArea.execute(sqlViewMemberAndViewArea, [user_id])
        if sqlViewMemberAndViewAreaTotalCount:
            is_member = 1
            curViewMemberAndViewAreaResult = curViewMemberAndViewArea.fetchone()
            # print "curViewMemberAndViewAreaResult   ",curViewMemberAndViewAreaResult
            member_name = curViewMemberAndViewAreaResult[1]
            member_email = curViewMemberAndViewAreaResult[2]
            member_province = curViewMemberAndViewAreaResult[3]
            member_city = curViewMemberAndViewAreaResult[4]
            member_district = curViewMemberAndViewAreaResult[5]
            member_address = curViewMemberAndViewAreaResult[6]
        else:
            is_member = 0
            member_name = ""
            member_email = ""
            member_province = ""
            member_city = ""
            member_district = ""
            member_address = ""

        curViewMemberAndViewArea.close()
        # 27
        curItem = self.conn.cursor()
        curItemSQL = "SELECT item_title,item_content,item_url,item_address,item_publishdate, item_deadline,item_handlername,item_handlerphone,dept_id,service_id FROM zzh_item WHERE item_id=%s"
        curItemSQLTotalCount = curItem.execute(curItemSQL, [item_id])
        if curItemSQLTotalCount:
            curItemSQLResult = curItem.fetchone()
            # print "curItemSQLResult   ", curItemSQLResult
            item_name = curItemSQLResult[0]
            item_content = curItemSQLResult[1]
            item_url = curItemSQLResult[2]
            item_address = curItemSQLResult[3]
            item_publishdate = curItemSQLResult[4]
            item_deadline = curItemSQLResult[5]
            item_handlername = curItemSQLResult[6]
            item_handlerphone = curItemSQLResult[7]

            dept_id = curItemSQLResult[8]

            service_id = curItemSQLResult[9]
        else:
            item_name = ""
            item_content = ""
            item_url = ""
            item_address = ""
            item_publishdate = ""
            item_deadline = ""
            item_handlername = ""
            item_handlerphone = ""
            dept_id = 0
            service_id = 0
            errorMessage = "没有符合itemId的记录，请检查item是否存在"
            return errorMessage
        curItem.close()
        # 37
        curService = self.conn.cursor()
        curServiceSQL = "SELECT service_name,service_code,service_price,service_rate,service_content,service_contract FROM zzh_service WHERE service_id=%s "
        curServiceSQLTotalCount = curService.execute(curServiceSQL, [service_id])
        if curServiceSQLTotalCount:
            curServiceSQLResult = curService.fetchone()
            # print "curServiceSQLResult   ", curServiceSQLResult


            service_name = curServiceSQLResult[0]
            service_code = curServiceSQLResult[1]
            service_price = curServiceSQLResult[2]

            service_rate = curServiceSQLResult[3]
            service_content = curServiceSQLResult[4]
            service_contract = curServiceSQLResult[5]

        else:
            service_name = ""
            service_code = ""
            service_price = 0

            service_rate = 0
            service_content = ""
            service_contract = ""
            errorMessage = "没有符合itemId绑定的serviceId的记录，请检查serviceId是否存在"
            return errorMessage
        curService.close()
        # 43
        if is_coupon:
            curCoupon = self.conn.cursor()
            curCouponSQL = "SELECT coupon_name,coupon_code,coupon_type,coupon_conditions,coupon_discount FROM zzh_coupon WHERE coupon_id = %s"
            curCouponSQLTotalCount = curCoupon.execute(curCouponSQL, [coupon_id])
            if curCouponSQLTotalCount:
                curCouponSQLResult = curCoupon.fetchone()
                # print "curCouponSQLResult   ", curCouponSQLResult
                coupon_name = curCouponSQLResult[0]
                coupon_code = curCouponSQLResult[1]
                coupon_type = curCouponSQLResult[2]
                coupon_conditions = curCouponSQLResult[3]
                coupon_discount = curCouponSQLResult[4]
                curCoupon.close()
            else:
                curCoupon.close()
                errorMessage = "没有符合coupon_id的优惠券，请检查coupon_id是否存在"
                return errorMessage
        else:
            coupon_name = ""
            coupon_code = ""
            coupon_type = 1
            coupon_conditions = 1
            coupon_discount = 1


            # 48
        # 获取到zzh_config表游标，读取后台管理员设定的发票比例invoice_rate
        curZZHConfig = self.conn.cursor()
        curZZHConfigSQL = "SELECT invoice_rate FROM zzh_config WHERE id=1"
        curZZHConfigTotalCount = curZZHConfig.execute(curZZHConfigSQL)
        curZZHConfigResult = curZZHConfig.fetchone()
        # print "curZZHConfigResult   ", curZZHConfigResult
        invoice_rate = curZZHConfigResult[0]
        curZZHConfig.close()

        is_invoice = Info[
            'is_invoice']  # 前台传递    是否索要发票，如果这个为1 ，则在zzh_invoice中插入记录。发票等到最后来处理，只有进行支付之后，才会写入zzh_invoice表，并开具发票
        if is_invoice:
            invoice_title = Info['invoice_title']
            invoice_taxes = invoice_rate * order_amount
        else:
            invoice_title = Info['invoice_title']
            invoice_taxes = 0

            # 50

        sql = 'INSERT INTO zzh_order (order_no,order_ip,order_type,trade_no,user_id,' \
              'user_name,payment_id,payment_fee,payment_status,    ' \
              ' order_created_time,is_invoice,invoice_title,invoice_taxes,  ' \
              ' payable_amount,real_amount,order_amount,order_point,is_member,   ' \
              ' member_name,member_email,member_telephone,member_province, member_city,  ' \
              '   member_district,member_address,is_coupon,coupon_name, coupon_code,  ' \
              '   coupon_type,coupon_conditions,coupon_discount,service_content, service_contract,   ' \
              '  service_code,service_name,service_price,service_rate, item_name,  ' \
              ' item_content,item_url,item_address,item_publishdate,item_deadline ,  ' \
              '  item_handlername,item_handlerphone,dept_id,order_status,order_product_is_success,order_message )' \
              ' VALUES ' \
              '  (%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s,' \
              '%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s ,%s)'
        cur = self.conn.cursor()
        rs = cur.execute(sql, [order_no, order_ip, order_type, trade_no,
                               user_id, user_name, payment_id, payment_fee, payment_status,
                               order_created_time, is_invoice, invoice_title, invoice_taxes,
                               payable_amount, real_amount, order_amount, order_point, is_member,
                               member_name, member_email, member_telephone, member_province, member_city,
                               member_district, member_address, is_coupon, coupon_name, coupon_code,
                               coupon_type, coupon_conditions, coupon_discount, service_content, service_contract,
                               service_code, service_name, service_price, service_rate, item_name,
                               item_content, item_url, item_address, item_publishdate, item_deadline,
                               item_handlername, item_handlerphone, dept_id, order_status, order_product_is_success,
                               order_message
                               ])
        self.conn.commit()

        sql2 = 'SELECT max(order_id) FROM zzh_order'
        rs2TC = cur.execute(sql2)
        rs2 = cur.fetchone()
        rs2Int = rs2[0]

        sql3 = "SELECT * FROM zzh_order WHERE order_id=%s"
        cur = self.conn.cursor()
        total_count = cur.execute(sql3, [rs2Int])
        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            userOauthJson = Table2JsonByYM.order_list_to_json(result)
            return total_count, userOauthJson
        else:
            cur.close()
            self.conn.close()
            return 0, None

        cur.close()
        self.conn.close()
        if rs:
            return rs
        else:
            return None

            # 增加了会员功能的插入order表的操作

            # lym insertOrder
    def insertOrder_Old2(self, Info, order_no):
        # 01-04

        # order_no = Info['order_no']    #前台传递

        order_type = Info['order_type']  # 前台传递 ,1会员订单 0项目订单 int型
        payable_amount = Info['payable_amount']  # 前台传递
        real_amount = Info['real_amount']  # 前台传递
        order_amount = Info['order_amount']  # 前台传递
        order_point = Info['order_point']
        #is_invoice = Info['is_invoice']
        #invoice_title = Info['invoice_title']
        payment_id = Info['payment_id']  # 写入支付zzh_payment，绑定order_id和payment_id
        is_coupon = Info['is_coupon']
        coupon_id = Info['coupon_id']  # 前台传递,根据这个读取zzh_coupon的信息
        user_id = Info['user_id']  # 根据这个读取zzh_user和zzh_member
        item_id = Info['item_id']  # item_id   绑定item

        # 暂时没用这些（设置了一些默认值，避免程序出错）：order_ip trade_no payment_fee payment_status order_status order_product_is_success order_message
        order_ip = ""
        trade_no = ""
        payment_fee = 0
        payment_status = 1
        order_status = 0
        order_product_is_success = 0
        order_message = ""

        payment_time = " "
        order_created_time = localTime  # 当前时间，订单创建时间


        curUser = self.conn.cursor()
        sqlUser = 'SELECT user_name,user_phone FROM zzh_user WHERE user_id=%s'
        curUserTotalCount = curUser.execute(sqlUser, [user_id])
        if curUserTotalCount:
            curuserNameResult = curUser.fetchone()

            user_name = curuserNameResult[0]  # 获取到用户名
            member_telephone = curuserNameResult[1]
        else:
            user_name = ""
            member_telephone = ""
            errorMessage = "没有符合userId的记录，请检查是否注册"
            return 0,errorMessage
        curUser.close()

        #会员订单LYM，member相关字段插入order表
        if order_type == 1:
            curViewMemberAndViewArea = self.conn.cursor()
            sqlViewMemberAndViewArea = 'SELECT member_id,member_name,member_email,province_id,city_id,district_id,member_address  FROM zzh_member WHERE user_id = %s '
            sqlViewMemberAndViewAreaTotalCount = curViewMemberAndViewArea.execute(sqlViewMemberAndViewArea, [user_id])
            if sqlViewMemberAndViewAreaTotalCount:
                is_member = 1
                curViewMemberAndViewAreaResult = curViewMemberAndViewArea.fetchone()
                member_name = curViewMemberAndViewAreaResult[1]
                member_email = curViewMemberAndViewAreaResult[2]
                member_province = curViewMemberAndViewAreaResult[3]
                member_city = curViewMemberAndViewAreaResult[4]
                member_district = curViewMemberAndViewAreaResult[5]
                member_address = curViewMemberAndViewAreaResult[6]
            else:
                is_member = 0
                member_name = ""
                member_email = ""
                member_province = ""
                member_city = ""
                member_district = ""
                member_address = ""

            curViewMemberAndViewArea.close()

            if is_coupon:
                curCoupon = self.conn.cursor()
                curCouponSQL = "SELECT coupon_name,coupon_code,coupon_type,coupon_conditions,coupon_discount FROM zzh_coupon WHERE coupon_id = %s"
                curCouponSQLTotalCount = curCoupon.execute(curCouponSQL, [coupon_id])
                if curCouponSQLTotalCount:
                    curCouponSQLResult = curCoupon.fetchone()
                    # print "curCouponSQLResult   ", curCouponSQLResult
                    coupon_name = curCouponSQLResult[0]
                    coupon_code = curCouponSQLResult[1]
                    coupon_type = curCouponSQLResult[2]
                    coupon_conditions = curCouponSQLResult[3]
                    coupon_discount = curCouponSQLResult[4]
                    curCoupon.close()
                else:
                    curCoupon.close()
                    errorMessage = "没有符合coupon_id的优惠券，请检查coupon_id是否存在"
                    return 0, errorMessage
            else:
                coupon_name = ""
                coupon_code = ""
                coupon_type = 1
                coupon_conditions = 1
                coupon_discount = 1


                # 48
            # 获取到zzh_config表游标，读取后台管理员设定的发票比例invoice_rate
            curZZHConfig = self.conn.cursor()
            curZZHConfigSQL = "SELECT invoice_rate FROM zzh_config WHERE id=1"
            curZZHConfigTotalCount = curZZHConfig.execute(curZZHConfigSQL)
            curZZHConfigResult = curZZHConfig.fetchone()
            # print "curZZHConfigResult   ", curZZHConfigResult
            invoice_rate = float(curZZHConfigResult[0])
            curZZHConfig.close()

            is_invoice = Info['is_invoice']  # 前台传递    是否索要发票，如果这个为1 ，则在zzh_invoice中插入记录。发票等到最后来处理，只有进行支付之后，才会写入zzh_invoice表，并开具发票
            if is_invoice:
                invoice_title = Info['invoice_title']
                invoice_taxes = invoice_rate * float(order_amount)
            else:
                invoice_title = Info['invoice_title']
                invoice_taxes = 0

                # 50

            sql = 'INSERT INTO zzh_order (order_no, order_ip, order_type, trade_no,' \
                  'user_id, user_name, payment_id, payment_fee, ' \
                  'payment_status, order_created_time, is_invoice, invoice_title, ' \
                  'invoice_taxes, payable_amount, real_amount, order_amount, ' \
                  'order_point, is_member,member_name, member_email, ' \
                  'member_telephone, member_province, member_city,member_district, ' \
                  'member_address, is_coupon, coupon_name, coupon_code,' \
                  'coupon_type,coupon_conditions, coupon_discount,order_status, ' \
                  'order_product_is_success,order_message, item_id )' \
                  ' VALUES ' \
                  '  (%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s ,%s)'
            cur = self.conn.cursor()
            rs = cur.execute(sql, [order_no, order_ip, order_type, trade_no,
                                   user_id, user_name, payment_id, payment_fee,
                                   payment_status, order_created_time, is_invoice, invoice_title,
                                   invoice_taxes, payable_amount, real_amount, order_amount,
                                   order_point, is_member,member_name, member_email,
                                   member_telephone, member_province, member_city,member_district,
                                   member_address, is_coupon, coupon_name, coupon_code,
                                   coupon_type,coupon_conditions, coupon_discount,order_status,
                                   order_product_is_success,order_message, item_id
                                   ])
            self.conn.commit()

            sql2 = 'SELECT max(order_id) FROM zzh_order'
            rs2TC = cur.execute(sql2)
            rs2 = cur.fetchone()
            rs2Int = rs2[0]

            sql3 = "SELECT * FROM zzh_order WHERE order_id=%s"
            cur = self.conn.cursor()
            total_count = cur.execute(sql3, [rs2Int])
            if total_count:

                result = cur.fetchall()
                cur.close()
                self.conn.close()
                userOauthJson = Table2JsonByYM.order_list_to_json(result)

                return (total_count, userOauthJson)
            else:
                cur.close()
                self.conn.close()

                return (0, None)




        #非会员订单,member表 item表相关的字段插入order表
        if order_type == 2:
            # 20

            curViewMemberAndViewArea = self.conn.cursor()
            sqlViewMemberAndViewArea = 'SELECT member_id,member_name,member_email,province_id,city_id,district_id,member_address  FROM zzh_member WHERE user_id = %s '
            sqlViewMemberAndViewAreaTotalCount = curViewMemberAndViewArea.execute(sqlViewMemberAndViewArea, [user_id])
            if sqlViewMemberAndViewAreaTotalCount:
                is_member = 1
                curViewMemberAndViewAreaResult = curViewMemberAndViewArea.fetchone()
                member_name = curViewMemberAndViewAreaResult[1]
                member_email = curViewMemberAndViewAreaResult[2]
                member_province = curViewMemberAndViewAreaResult[3]
                member_city = curViewMemberAndViewAreaResult[4]
                member_district = curViewMemberAndViewAreaResult[5]
                member_address = curViewMemberAndViewAreaResult[6]
            else:
                is_member = 0
                member_name = ""
                member_email = ""
                member_province = ""
                member_city = ""
                member_district = ""
                member_address = ""

            curViewMemberAndViewArea.close()
            # 27
            curItem = self.conn.cursor()
            curItemSQL = "SELECT item_title,item_content,item_url,item_address,item_publishdate, item_deadline,item_handlername,item_handlerphone,dept_id,service_id FROM zzh_item WHERE item_id=%s"
            curItemSQLTotalCount = curItem.execute(curItemSQL, [item_id])
            if curItemSQLTotalCount:
                curItemSQLResult = curItem.fetchone()
                # print "curItemSQLResult   ", curItemSQLResult
                item_name = curItemSQLResult[0]
                item_content = curItemSQLResult[1]
                item_url = curItemSQLResult[2]
                item_address = curItemSQLResult[3]
                item_publishdate = curItemSQLResult[4]
                item_deadline = curItemSQLResult[5]
                item_handlername = curItemSQLResult[6]
                item_handlerphone = curItemSQLResult[7]

                dept_id = curItemSQLResult[8]

                service_id = curItemSQLResult[9]
            else:
                item_name = ""
                item_content = ""
                item_url = ""
                item_address = ""
                item_publishdate = ""
                item_deadline = ""
                item_handlername = ""
                item_handlerphone = ""
                dept_id = 0
                service_id = 0
                errorMessage = "没有符合itemId的记录，请检查item是否存在"
                return 0, errorMessage
            curItem.close()
            # 37
            curService = self.conn.cursor()
            curServiceSQL = "SELECT service_name,service_code,service_price,service_rate,service_content,service_contract FROM zzh_service WHERE service_id=%s "
            curServiceSQLTotalCount = curService.execute(curServiceSQL, [service_id])
            if curServiceSQLTotalCount:
                curServiceSQLResult = curService.fetchone()
                # print "curServiceSQLResult   ", curServiceSQLResult


                service_name = curServiceSQLResult[0]
                service_code = curServiceSQLResult[1]
                service_price = curServiceSQLResult[2]

                service_rate = curServiceSQLResult[3]
                service_content = curServiceSQLResult[4]
                service_contract = curServiceSQLResult[5]

            else:
                service_name = ""
                service_code = ""
                service_price = 0

                service_rate = 0
                service_content = ""
                service_contract = ""
                errorMessage = "没有符合itemId绑定的serviceId的记录，请检查serviceId是否存在"
                return 0, errorMessage
            curService.close()
            # 43
            if is_coupon:
                curCoupon = self.conn.cursor()
                curCouponSQL = "SELECT coupon_name,coupon_code,coupon_type,coupon_conditions,coupon_discount FROM zzh_coupon WHERE coupon_id = %s"
                curCouponSQLTotalCount = curCoupon.execute(curCouponSQL, [coupon_id])
                if curCouponSQLTotalCount:
                    curCouponSQLResult = curCoupon.fetchone()
                    # print "curCouponSQLResult   ", curCouponSQLResult
                    coupon_name = curCouponSQLResult[0]
                    coupon_code = curCouponSQLResult[1]
                    coupon_type = curCouponSQLResult[2]
                    coupon_conditions = curCouponSQLResult[3]
                    coupon_discount = curCouponSQLResult[4]
                    curCoupon.close()
                else:
                    curCoupon.close()
                    errorMessage = "没有符合coupon_id的优惠券，请检查coupon_id是否存在"
                    return 0, errorMessage
            else:
                coupon_name = ""
                coupon_code = ""
                coupon_type = 1
                coupon_conditions = 1
                coupon_discount = 1


                # 48
            # 获取到zzh_config表游标，读取后台管理员设定的发票比例invoice_rate
            curZZHConfig = self.conn.cursor()
            curZZHConfigSQL = "SELECT invoice_rate FROM zzh_config WHERE id=1"
            curZZHConfigTotalCount = curZZHConfig.execute(curZZHConfigSQL)
            curZZHConfigResult = curZZHConfig.fetchone()
            # print "curZZHConfigResult   ", curZZHConfigResult
            invoice_rate = float(curZZHConfigResult[0])
            curZZHConfig.close()

            is_invoice = Info[
                'is_invoice']  # 前台传递    是否索要发票，如果这个为1 ，则在zzh_invoice中插入记录。发票等到最后来处理，只有进行支付之后，才会写入zzh_invoice表，并开具发票
            if is_invoice:
                invoice_title = Info['invoice_title']
                invoice_taxes = invoice_rate * float(order_amount)
            else:
                invoice_title = Info['invoice_title']
                invoice_taxes = 0

                # 50

            sql = 'INSERT INTO zzh_order (order_no,order_ip,order_type,trade_no,user_id,' \
                  'user_name,payment_id,payment_fee,payment_status,    ' \
                  ' order_created_time,is_invoice,invoice_title,invoice_taxes,  ' \
                  ' payable_amount,real_amount,order_amount,order_point,is_member,   ' \
                  ' member_name,member_email,member_telephone,member_province, member_city,  ' \
                  '   member_district,member_address,is_coupon,coupon_name, coupon_code,  ' \
                  '   coupon_type,coupon_conditions,coupon_discount,service_content, service_contract,   ' \
                  '  service_code,service_name,service_price,service_rate, item_name,  ' \
                  ' item_content,item_url,item_address,item_publishdate,item_deadline ,  ' \
                  '  item_handlername,item_handlerphone,dept_id,order_status,order_product_is_success,order_message, item_id )' \
                  ' VALUES ' \
                  '  (%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s ,%s, %s)'
            cur = self.conn.cursor()
            rs = cur.execute(sql, [order_no, order_ip, order_type, trade_no,
                                   user_id, user_name, payment_id, payment_fee, payment_status,
                                   order_created_time, is_invoice, invoice_title, invoice_taxes,
                                   payable_amount, real_amount, order_amount, order_point, is_member,
                                   member_name, member_email, member_telephone, member_province, member_city,
                                   member_district, member_address, is_coupon, coupon_name, coupon_code,
                                   coupon_type, coupon_conditions, coupon_discount, service_content, service_contract,
                                   service_code, service_name, service_price, service_rate, item_name,
                                   item_content, item_url, item_address, item_publishdate, item_deadline,
                                   item_handlername, item_handlerphone, dept_id, order_status, order_product_is_success,
                                   order_message, item_id
                                   ])
            self.conn.commit()

            sql2 = 'SELECT max(order_id) FROM zzh_order'
            rs2TC = cur.execute(sql2)
            rs2 = cur.fetchone()
            rs2Int = rs2[0]

            sql3 = "SELECT * FROM zzh_order WHERE order_id=%s"
            cur = self.conn.cursor()
            total_count = cur.execute(sql3, [rs2Int])
            if total_count:
                result = cur.fetchall()
                cur.close()
                self.conn.close()
                userOauthJson = Table2JsonByYM.order_list_to_json(result)

                return (total_count, userOauthJson)

            else:
                cur.close()
                self.conn.close()
                return (0, None)

            # cur.close()
            # self.conn.close()
    def insertOrder(self, Info, order_no):
        # 01-04

        # order_no = Info['order_no']    #前台传递
        order_type = Info['order_type']  # 前台传递 ,1会员订单 0项目订单 int型
        payable_amount = Info['payable_amount']  # 前台传递
        real_amount = Info['real_amount']  # 前台传递
        order_amount = Info['order_amount']  # 前台传递
        order_point = Info['order_point']
        is_invoice = Info['is_invoice']
        invoice_title = Info['invoice_title']
        payment_id = Info['payment_id']  # 写入支付zzh_payment，绑定order_id和payment_id
        is_coupon = Info['is_coupon']
        coupon_id = Info['coupon_code']  # 前台传递,根据这个读取zzh_coupon的信息
        user_id = Info['user_id']  # 根据这个读取zzh_user和zzh_member
        item_id = Info['item_id']  # item_id   绑定item
        item_name = Info['item_name']

        # 暂时没用这些（设置了一些默认值，避免程序出错）：order_ip trade_no payment_fee payment_status order_status order_product_is_success order_message
        order_ip = Info['order_ip']
        trade_no = Info['trade_no']
        payment_fee = Info['payment_fee']
        payment_status = Info['payment_status']
        order_status = Info['order_status']
        order_product_is_success = Info['order_product_is_success']
        order_message = Info['order_message']

        payment_time = Info['payment_time']

        order_created_time = localTime  # 当前时间，订单创建时间


        curUser = self.conn.cursor()
        sqlUser = 'SELECT user_name,user_phone FROM zzh_user WHERE user_id=%s'
        curUserTotalCount = curUser.execute(sqlUser, [user_id])
        if curUserTotalCount:
            curuserNameResult = curUser.fetchone()

            user_name = curuserNameResult[0]  # 获取到用户名
            member_telephone = curuserNameResult[1]
        else:
            user_name = ""
            member_telephone = ""
            errorMessage = "没有符合userId的记录，请检查是否注册"
            return 0, errorMessage
        curUser.close()

        #会员订单LYM，member相关字段插入order表
        if order_type == 1:
            curViewMemberAndViewArea = self.conn.cursor()
            sqlViewMemberAndViewArea = 'SELECT member_id,member_name,member_email,province_id,city_id,district_id,member_address  FROM zzh_member WHERE user_id = %s '
            sqlViewMemberAndViewAreaTotalCount = curViewMemberAndViewArea.execute(sqlViewMemberAndViewArea, [user_id])
            if sqlViewMemberAndViewAreaTotalCount:
                is_member = 1
                curViewMemberAndViewAreaResult = curViewMemberAndViewArea.fetchone()
                member_name = curViewMemberAndViewAreaResult[1]
                member_email = curViewMemberAndViewAreaResult[2]
                member_province = curViewMemberAndViewAreaResult[3]
                member_city = curViewMemberAndViewAreaResult[4]
                member_district = curViewMemberAndViewAreaResult[5]
                member_address = curViewMemberAndViewAreaResult[6]
            else:
                is_member = 0
                member_name = ""
                member_email = ""
                member_province = ""
                member_city = ""
                member_district = ""
                member_address = ""

            curViewMemberAndViewArea.close()

            if is_coupon:
                curCoupon = self.conn.cursor()
                curCouponSQL = "SELECT coupon_name,coupon_code,coupon_type,coupon_conditions,coupon_discount FROM zzh_coupon WHERE coupon_id = %s"
                curCouponSQLTotalCount = curCoupon.execute(curCouponSQL, [coupon_id])
                if curCouponSQLTotalCount:
                    curCouponSQLResult = curCoupon.fetchone()
                    # print "curCouponSQLResult   ", curCouponSQLResult
                    coupon_name = curCouponSQLResult[0]
                    coupon_code = curCouponSQLResult[1]
                    coupon_type = curCouponSQLResult[2]
                    coupon_conditions = curCouponSQLResult[3]
                    coupon_discount = curCouponSQLResult[4]
                    curCoupon.close()
                else:
                    curCoupon.close()
                    errorMessage = "没有符合coupon_id的优惠券，请检查coupon_id是否存在"
                    return 0, errorMessage
            else:
                coupon_name = ""
                coupon_code = ""
                coupon_type = 1
                coupon_conditions = 1
                coupon_discount = 1


                # 48
            # 获取到zzh_config表游标，读取后台管理员设定的发票比例invoice_rate
            curZZHConfig = self.conn.cursor()
            curZZHConfigSQL = "SELECT invoice_rate FROM zzh_config WHERE id=1"
            curZZHConfigTotalCount = curZZHConfig.execute(curZZHConfigSQL)
            curZZHConfigResult = curZZHConfig.fetchone()
            # print "curZZHConfigResult   ", curZZHConfigResult
            invoice_rate = float(curZZHConfigResult[0])
            curZZHConfig.close()

            is_invoice = Info['is_invoice']  # 前台传递    是否索要发票，如果这个为1 ，则在zzh_invoice中插入记录。发票等到最后来处理，只有进行支付之后，才会写入zzh_invoice表，并开具发票
            if is_invoice:
                invoice_title = Info['invoice_title']
                invoice_taxes = invoice_rate * float(order_amount)
            else:
                invoice_title = Info['invoice_title']
                invoice_taxes = 0

                # 50

            sql = 'INSERT INTO zzh_order (order_no, order_ip, order_type, trade_no,' \
                  'user_id, user_name, payment_id, payment_fee, ' \
                  'payment_status, order_created_time, is_invoice, invoice_title, ' \
                  'invoice_taxes, payable_amount, real_amount, order_amount, ' \
                  'order_point, is_member,member_name, member_email, ' \
                  'member_telephone, member_province, member_city,member_district, ' \
                  'member_address, is_coupon, coupon_name, coupon_code,' \
                  'coupon_type,coupon_conditions, coupon_discount,order_status, ' \
                  'order_product_is_success,order_message, item_id, item_name )' \
                  ' VALUES ' \
                  '  (%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s ,%s, %s)'
            cur = self.conn.cursor()
            rs = cur.execute(sql, [order_no, order_ip, order_type, trade_no,
                                   user_id, user_name, payment_id, payment_fee,
                                   payment_status, order_created_time, is_invoice, invoice_title,
                                   invoice_taxes, payable_amount, real_amount, order_amount,
                                   order_point, is_member,member_name, member_email,
                                   member_telephone, member_province, member_city,member_district,
                                   member_address, is_coupon, coupon_name, coupon_code,
                                   coupon_type,coupon_conditions, coupon_discount,order_status,
                                   order_product_is_success,order_message, item_id, item_name
                                   ])
            self.conn.commit()

            sql2 = 'SELECT max(order_id) FROM zzh_order'
            rs2TC = cur.execute(sql2)
            rs2 = cur.fetchone()
            rs2Int = rs2[0]

            sql3 = "SELECT * FROM zzh_order WHERE order_id=%s"
            cur = self.conn.cursor()
            total_count = cur.execute(sql3, [rs2Int])
            if total_count:
                result = cur.fetchall()
                cur.close()
                self.conn.close()
                userOauthJson = Table2JsonByYM.order_list_to_json(result)
                return total_count, userOauthJson
            else:
                cur.close()
                self.conn.close()
                return 0, None

        #非会员订单,member表 item表相关的字段插入order表
        if order_type == 2:
            # 20

            curViewMemberAndViewArea = self.conn.cursor()
            sqlViewMemberAndViewArea = 'SELECT member_id,member_name,member_email,province_id,city_id,district_id,member_address  FROM zzh_member WHERE user_id = %s '
            sqlViewMemberAndViewAreaTotalCount = curViewMemberAndViewArea.execute(sqlViewMemberAndViewArea, [user_id])
            if sqlViewMemberAndViewAreaTotalCount:
                is_member = 1
                curViewMemberAndViewAreaResult = curViewMemberAndViewArea.fetchone()
                member_name = curViewMemberAndViewAreaResult[1]
                member_email = curViewMemberAndViewAreaResult[2]
                member_province = curViewMemberAndViewAreaResult[3]
                member_city = curViewMemberAndViewAreaResult[4]
                member_district = curViewMemberAndViewAreaResult[5]
                member_address = curViewMemberAndViewAreaResult[6]
            else:
                is_member = 0
                member_name = ""
                member_email = ""
                member_province = ""
                member_city = ""
                member_district = ""
                member_address = ""

            curViewMemberAndViewArea.close()
            # 27
            curItem = self.conn.cursor()
            curItemSQL = "SELECT item_title,item_content,item_url,item_address,item_publishdate, item_deadline,item_handlername,item_handlerphone,dept_id,service_id FROM zzh_item WHERE item_id=%s"
            curItemSQLTotalCount = curItem.execute(curItemSQL, [item_id])
            if curItemSQLTotalCount:
                curItemSQLResult = curItem.fetchone()
                # print "curItemSQLResult   ", curItemSQLResult
                item_name = curItemSQLResult[0]
                item_content = curItemSQLResult[1]
                item_url = curItemSQLResult[2]
                item_address = curItemSQLResult[3]
                item_publishdate = curItemSQLResult[4]
                item_deadline = curItemSQLResult[5]
                item_handlername = curItemSQLResult[6]
                item_handlerphone = curItemSQLResult[7]

                dept_id = curItemSQLResult[8]

                service_id = curItemSQLResult[9]
            else:
                item_name = ""
                item_content = ""
                item_url = ""
                item_address = ""
                item_publishdate = ""
                item_deadline = ""
                item_handlername = ""
                item_handlerphone = ""
                dept_id = 0
                service_id = 0
                errorMessage = "没有符合itemId的记录，请检查item是否存在"
                return 0, errorMessage
            curItem.close()
            # 37
            curService = self.conn.cursor()
            curServiceSQL = "SELECT service_name,service_code,service_price,service_rate,service_content,service_contract FROM zzh_service WHERE service_id=%s "
            curServiceSQLTotalCount = curService.execute(curServiceSQL, [service_id])
            if curServiceSQLTotalCount:
                curServiceSQLResult = curService.fetchone()
                # print "curServiceSQLResult   ", curServiceSQLResult


                service_name = curServiceSQLResult[0]
                service_code = curServiceSQLResult[1]
                service_price = curServiceSQLResult[2]

                service_rate = curServiceSQLResult[3]
                service_content = curServiceSQLResult[4]
                service_contract = curServiceSQLResult[5]

            else:
                service_name = ""
                service_code = ""
                service_price = 0

                service_rate = 0
                service_content = ""
                service_contract = ""
                errorMessage = "没有符合itemId绑定的serviceId的记录，请检查serviceId是否存在"
                return 0, errorMessage
            curService.close()
            # 43
            if is_coupon:
                curCoupon = self.conn.cursor()
                curCouponSQL = "SELECT coupon_name,coupon_code,coupon_type,coupon_conditions,coupon_discount FROM zzh_coupon WHERE coupon_id = %s"
                curCouponSQLTotalCount = curCoupon.execute(curCouponSQL, [coupon_id])
                if curCouponSQLTotalCount:
                    curCouponSQLResult = curCoupon.fetchone()
                    # print "curCouponSQLResult   ", curCouponSQLResult
                    coupon_name = curCouponSQLResult[0]
                    coupon_code = curCouponSQLResult[1]
                    coupon_type = curCouponSQLResult[2]
                    coupon_conditions = curCouponSQLResult[3]
                    coupon_discount = curCouponSQLResult[4]
                    curCoupon.close()
                else:
                    curCoupon.close()
                    errorMessage = "没有符合coupon_id的优惠券，请检查coupon_id是否存在"
                    return 0, errorMessage
            else:
                coupon_name = ""
                coupon_code = ""
                coupon_type = 1
                coupon_conditions = 1
                coupon_discount = 1


                # 48
            # 获取到zzh_config表游标，读取后台管理员设定的发票比例invoice_rate
            curZZHConfig = self.conn.cursor()
            curZZHConfigSQL = "SELECT invoice_rate FROM zzh_config WHERE id=1"
            curZZHConfigTotalCount = curZZHConfig.execute(curZZHConfigSQL)
            curZZHConfigResult = curZZHConfig.fetchone()
            # print "curZZHConfigResult   ", curZZHConfigResult
            invoice_rate = float(curZZHConfigResult[0])
            curZZHConfig.close()

            is_invoice = Info[
                'is_invoice']  # 前台传递    是否索要发票，如果这个为1 ，则在zzh_invoice中插入记录。发票等到最后来处理，只有进行支付之后，才会写入zzh_invoice表，并开具发票
            if is_invoice:
                invoice_title = Info['invoice_title']
                invoice_taxes = invoice_rate * float(order_amount)
            else:
                invoice_title = Info['invoice_title']
                invoice_taxes = 0

                # 50

            sql = 'INSERT INTO zzh_order (order_no,order_ip,order_type,trade_no,user_id,' \
                  'user_name,payment_id,payment_fee,payment_status,    ' \
                  ' order_created_time,is_invoice,invoice_title,invoice_taxes,  ' \
                  ' payable_amount,real_amount,order_amount,order_point,is_member,   ' \
                  ' member_name,member_email,member_telephone,member_province, member_city,  ' \
                  '   member_district,member_address,is_coupon,coupon_name, coupon_code,  ' \
                  '   coupon_type,coupon_conditions,coupon_discount,service_content, service_contract,   ' \
                  '  service_code,service_name,service_price,service_rate, item_name,  ' \
                  ' item_content,item_url,item_address,item_publishdate,item_deadline ,  ' \
                  '  item_handlername,item_handlerphone,dept_id,order_status,order_product_is_success,order_message, item_id )' \
                  ' VALUES ' \
                  '  (%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s ,%s, %s)'
            cur = self.conn.cursor()
            rs = cur.execute(sql, [order_no, order_ip, order_type, trade_no,
                                   user_id, user_name, payment_id, payment_fee, payment_status,
                                   order_created_time, is_invoice, invoice_title, invoice_taxes,
                                   payable_amount, real_amount, order_amount, order_point, is_member,
                                   member_name, member_email, member_telephone, member_province, member_city,
                                   member_district, member_address, is_coupon, coupon_name, coupon_code,
                                   coupon_type, coupon_conditions, coupon_discount, service_content, service_contract,
                                   service_code, service_name, service_price, service_rate, item_name,
                                   item_content, item_url, item_address, item_publishdate, item_deadline,
                                   item_handlername, item_handlerphone, dept_id, order_status, order_product_is_success,
                                   order_message, item_id
                                   ])
            self.conn.commit()

            sql2 = 'SELECT max(order_id) FROM zzh_order'
            rs2TC = cur.execute(sql2)
            rs2 = cur.fetchone()
            rs2Int = rs2[0]

            sql3 = "SELECT * FROM zzh_order WHERE order_id=%s"
            cur = self.conn.cursor()
            total_count = cur.execute(sql3, [rs2Int])
            if total_count:
                result = cur.fetchall()
                cur.close()
                self.conn.close()
                userOauthJson = Table2JsonByYM.order_list_to_json(result)
                return total_count, userOauthJson
            else:
                cur.close()
                self.conn.close()
                return 0, None

            # cur.close()
            # self.conn.close()

    #根据order_no查询order，v3是根据id
    def selectOrder(self, order_no):
        """
        根据ID查询信息，返回元组

        """
        sql = "select * from zzh_order WHERE order_no=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [order_no])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr

    #根据order_no更新，v3是根据order_id
    def updateOrder(self, Info):  #

        id = Info['order_id']  # 必填，请传递
        # 01-04
        order_no = Info['order_no']
        order_ip = Info['order_ip']  #
        order_type = Info['order_type']
        trade_no = Info['trade_no']

        # 05-09
        user_id = Info['user_id']  #
        user_name = Info['user_name']
        payment_id = Info['payment_id']  #
        payment_fee = Info['payment_fee']
        payment_status = Info['payment_status']

        # 10-14
        payment_time = Info['payment_time']  #
        order_created_time = localTime
        is_invoice = Info['is_invoice']  #
        invoice_title = Info['invoice_title']
        invoice_taxes = Info['invoice_taxes']

        # 15-19
        payable_amount = Info['payable_amount']  #
        real_amount = Info['real_amount']
        order_amount = Info['order_amount']  #
        order_point = Info['order_point']
        is_member = Info['is_member']

        # 20-24
        member_name = Info['member_name']  #
        member_email = Info['member_email']
        member_telephone = Info['member_telephone']  #
        member_province = Info['member_province']
        member_city = Info['member_city']

        # 25-29
        member_district = Info['member_district']  #
        member_address = Info['member_address']
        is_coupon = Info['is_coupon']  #
        coupon_name = Info['coupon_name']
        coupon_code = Info['coupon_code']

        # 30-34
        coupon_type = Info['coupon_type']  #
        coupon_conditions = Info['coupon_conditions']
        coupon_discount = Info['coupon_discount']  #
        service_content = Info['service_content']
        service_contract = Info['service_contract']

        # 35-39
        service_code = Info['service_code']  #
        service_name = Info['service_name']
        service_price = Info['service_price']  #
        service_rate = Info['service_rate']
        item_name = Info['item_name']

        # 40-44
        item_content = Info['item_content']  #
        item_url = Info['item_url']
        item_address = Info['item_address']  #
        item_publishdate = Info['item_publishdate']
        item_deadline = Info['item_deadline']

        # 45-49
        item_handlername = Info['item_handlername']  #
        item_handlerphone = Info['item_handlerphone']
        dept_id = Info['dept_id']  #
        order_status = Info['order_status']
        order_product_is_success = Info['order_product_is_success']
        order_message = Info['order_message']

        cur = self.conn.cursor()
        sql = 'UPDATE zzh_order SET ' \
              '             order_no=%s,order_ip=%s,order_type=%s,trade_no=%s, ' \
 \
              'user_id=%s,user_name =%s, payment_id =%s , payment_fee =%s  ,payment_status=%s,' \
 \
              '      payment_time=%s,order_created_time=%s,is_invoice=%s,invoice_title=%s,invoice_taxes=%s,' \
 \
              '    payable_amount=%s,real_amount=%s,order_amount=%s,order_point=%s,is_member=%s,   ' \
 \
              '     member_name=%s,member_email=%s,member_telephone=%s,member_province=%s, member_city=%s,  ' \
 \
              '   member_district=%s,member_address=%s,is_coupon=%s,coupon_name=%s, coupon_code=%s,  ' \
 \
              '   coupon_type=%s,coupon_conditions=%s,coupon_discount=%s,service_content=%s, service_contract=%s,  ' \
 \
              '   service_code=%s,service_name=%s,service_price=%s,service_rate=%s, item_name=%s,  ' \
 \
              '   item_content=%s,item_url=%s,item_address=%s,item_publishdate=%s,item_deadline=%s , ' \
 \
              '   item_handlername=%s,item_handlerphone=%s,dept_id=%s,order_status=%s,order_product_is_success=%s,order_message=%s ' \
              'WHERE order_no=%s'

        rs = cur.execute(sql, [order_no, order_ip, order_type, trade_no,
                               user_id, user_name, payment_id, payment_fee, payment_status,
                               payment_time, order_created_time, is_invoice, invoice_title, invoice_taxes,
                               payable_amount, real_amount, order_amount, order_point, is_member,
                               member_name, member_email, member_telephone, member_province, member_city,
                               member_district, member_address, is_coupon, coupon_name, coupon_code,
                               coupon_type, coupon_conditions, coupon_discount, service_content, service_contract,
                               service_code, service_name, service_price, service_rate, item_name,
                               item_content, item_url, item_address, item_publishdate, item_deadline,
                               item_handlername, item_handlerphone, dept_id, order_status, order_product_is_success,
                               order_message,
                               order_no])

        self.conn.commit()
        cur.close()
        self.conn.close()
        if rs:
            return rs
        else:
            return None

    def deleteOrder(self, ids):
        """
        根据ID删除数据表信息，ID需要以数组形式传递
        :param adminIdArray: id 构成的数组 []
        :return:
        """
        sql = "delete from zzh_order WHERE order_id=%s"
        cur = self.conn.cursor()
        for id in ids:
            rs = cur.execute(sql, [id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def removeOrder(self, ids, userId):
        sql = "UPDATE   zzh_order  SET order_status=-1 WHERE order_id=%s AND user_id=%s"
        cur = self.conn.cursor()
        for id in ids:
            rs = cur.execute(sql, [id, userId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs


#v3 后台管理的dao层操作
class OrderDaoV3(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def queryOrderById(self, id):
        """
        根据ID查询信息    查询    zzh_user
        :param :
        :return:
        单体查询，按照id查询
        """
        sql = "SELECT * FROM zzh_order WHERE order_id=%s"
        cur = self.conn.cursor()
        total_count = cur.execute(sql, [id])
        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            userOauthJson = Table2JsonByYM.order_list_to_json(result)
            return total_count, userOauthJson
        else:
            cur.close()
            self.conn.close()
            return 0, None

    def queryOrderByOrderNo(self, id):
        """
        根据ID查询信息    查询    zzh_user
        :param :
        :return:
        单体查询，按照id查询
        """
        sql = "SELECT * FROM zzh_order WHERE order_no=%s"
        cur = self.conn.cursor()
        total_count = cur.execute(sql, [id])
        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            userOauthJson = Table2JsonByYM.order_list_to_json(result)
            return total_count, userOauthJson
        else:
            cur.close()
            self.conn.close()
            return 0, None

    def queryOrderByCondition(self, **kwargs):
        """
        条件查询  zzh_user_oauth
        :param :
        :return:
        此处使用userId为条件查询
        """

        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        # 获取对应的映射字典
        fieldMap = fieldMap_Luo.orderAndDepartmentMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM view_orderAndDepartment WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM view_orderAndDepartment WHERE " + whereSentence

        cur = self.conn.cursor()
        total_count = cur.execute(sql)

        if total_count:
            adinfo = cur.fetchall()
            lists = MyPaging().getPage(total_count, pageIndex, pageSize, adinfo)
            adJson = Table2JsonLuo.view_orderAndDept_json(lists)
            return adJson, total_count
        else:
            return None, 0


            # def random_str(randomlength=32):
            #         str = ''
            #         chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
            #         length = len(chars) - 1
            #         random = Random()
            #         for i in range(32):
            #             str += chars[random.randint(0, length)]
            #         return str

    def random_str(self):
        array = ""
        for i in range(0, 31):
            j = random.randint(0, 9)
            array = array + "" + str(j)
        return array

    def insertOrder_Old(self, Info, order_no):
        """
          # 1-4，查询不同的表的时候，做好容错
        'order_no': request.json.get('order_no'),  #前台传递
        'order_ip': request.remote_addr,##确定   读取request
        'order_type': request.json.get('order_type'),  #前台传递     支付类型，1-pc,2-mobile
        'payment_id': request.json.get('payment_id'),  #前台传递     将支付记录与order_id绑定       我该拿这个做什么，传入进来，payment表没有任何符合的数据。
         'user_id': request.json.get('user_id'),  #前台传递
         'coupon_id':request.json.get('coupon_id'),    #couponId  前台传递，用这个id区查询zzh_coupon表的相应记录，做好容错
         'payable_amount': request.json.get('payable_amount'),  #前台传递  应付金额
        'real_amount': request.json.get('real_amount'),  #前台传递  实际支付金额
        'order_amount': request.json.get('order_amount'),  #前台传递  订单总金额

        'trade_no': request.json.get('trade_no'),#???交易号担保支付用到  这个该从哪里取到  先置空

        # 5-9

        'user_name': request.json.get('user_name'),  #根据user_id从zzh_ueser中查询



        'payment_fee': request.json.get('payment_fee'),#？？？支付手续费，先不管，置为空，数据库默认是  0.00


        'payment_status': request.json.get('payment_status',1),#确定   支付状态1默认未支付     2已支付，支付时更新

        # 10-14
        'payment_time': request.json.get('payment_time',''),  #确定    支付时间，创建订单时候默认为空，然后 等到支付的时候再进行调用更新订单表进行更新
        'order_created_time': request.json.get('order_created_time'),  #确定   当前时间，


        'is_invoice': request.json.get('is_invoice',0),  #是否开具发票，是否索要发票 0否，1是
        'invoice_title': request.json.get('invoice_title'),#发票抬头，如果is_invoice为1，根据这个invoice_title和userId添加zzh_invoice记录，同时绑定到invoice_id到order_id
        'invoice_taxes': request.json.get('invoice_taxes'),#？？？这个 发票费用  该从哪里进行读取？

        # 15-19

        'order_point': request.json.get('order_point'),#？？？积分，这个积分将会和uesr_id和point_value积分值一起写入 zzh_point_log  这是收入的积分


        'is_member': request.json.get('is_member'),##确定    是否是VIP，默认0不是 ，1是.   根据user_id查询zzh_member表取出member相关数据，

        # 20-24
        'member_name': request.json.get('member_name'),  #确定
        'member_email': request.json.get('member_email'),  #确定
        'member_telephone': request.json.get('member_telephone'),  #确定
        'member_province': request.json.get('member_province'),#确定
        'member_city': request.json.get('member_city'),#确定

        # 25-29
        'member_district': request.json.get('member_district'),  #确定
        'member_address': request.json.get('member_address'),  #确定


        'is_coupon': request.json.get('is_coupon'),  #？？？优惠券，是否使用优惠券0否，1是  如果使用，则去查询zzh_coupon表？这个地方是不是需要绑定coupon_id？。如果不使用，是否直接置为空
        'coupon_name': request.json.get('coupon_name'),#？？？优惠券名称应该不是唯一的吧
        'coupon_code': request.json.get('coupon_code'),#？？？

        # 30-34
        'coupon_type': request.json.get('coupon_type'),  #？？？
        'coupon_conditions': request.json.get('coupon_conditions'),  #？？？
        'coupon_discount': request.json.get('coupon_discount'),  #？？？



        'service_content': request.json.get('service_content'),#确定   zzh_service表， item里面是有一个service_id字段的 根据item_id对应的service_id去查找这个服务的相关数据
        'service_contract': request.json.get('service_contract'),#确定

        # 35-39
        'service_code': request.json.get('service_code'),  #确定
        'service_name': request.json.get('service_name'),  #确定
        'service_price': request.json.get('service_price'),  #确定
        'service_rate': request.json.get('service_rate'),#确定


        'item_name': request.json.get('item_name'),#确定    根据item_id从zzh_item取出对应数据填充 40-47所有字段

        # 40-44
        'item_content': request.json.get('item_content'),  #确定
        'item_url': request.json.get('item_url'),  #确定
        'item_address': request.json.get('item_address'),  #确定
        'item_publishdate': request.json.get('item_publishdate'),#确定
        'item_deadline': request.json.get('item_deadline'),#确定

        # 45-49
        'item_handlername': request.json.get('item_handlername'),  #确定
        'item_handlerphone': request.json.get('item_handlerphone'),  #确定
        'dept_id': request.json.get('dept_id'),  #确定


        'order_status': request.json.get('order_status',0),#确定    订单状态，默认为0生成订单，之后的状态等到  后续操作的时候更新
        'order_product_is_success': request.json.get('order_product_is_success',0)#确定    产品服务是否成功，默认0未成功,   1表示成功，等服务完成之后进行更新


        """

        # 01-04
        order_message = Info['order_message']
        # order_no = Info['order_no']    #前台传递
        order_ip = Info['order_ip']  # request读取
        order_type = Info['order_type']  # 前台传递
        trade_no = Info['trade_no']  # 前台传递

        user_id = Info['user_id']  # 根据这个读取zzh_user和zzh_member

        payment_id = Info['payment_id']  # 写入支付zzh_payment，绑定order_id和payment_id
        payment_time = " "
        payment_fee = Info['payment_fee']  # 数据库默认为0   ，先置空不管
        item_id = Info['item_id']  # item_id   绑定item
        payment_status = Info['payment_status']  # 默认为1   未支付，    等到实际支付的时候进行更新状态为2
        payable_amount = Info['payable_amount']  # 前台传递
        real_amount = Info['real_amount']  # 前台传递

        order_amount = Info['order_amount']  # 前台传递
        order_created_time = localTime  # 当前时间，订单创建时间
        order_point = Info['order_amount']

        is_coupon = Info['is_coupon']

        coupon_id = Info['coupon_id']  # 前台传递,根据这个读取zzh_coupon的信息
        order_status = Info['order_status']  # 订单状态，默认为0生成订单，之后的状态等到  后续操作的时候更新
        order_product_is_success = Info['order_product_is_success']  # 产品服务是否成功，默认0未成功,   1表示成功，等服务完成之后进行更新
        # 18个
        curUser = self.conn.cursor()
        sqlUser = 'SELECT user_name,user_phone FROM zzh_user WHERE user_id=%s'
        curUserTotalCount = curUser.execute(sqlUser, [user_id])
        if curUserTotalCount:
            curuserNameResult = curUser.fetchone()

            user_name = curuserNameResult[0]  # 获取到用户名
            member_telephone = curuserNameResult[1]
        else:
            user_name = ""
            member_telephone = ""
            errorMessage = "没有符合userId的记录，请检查是否注册"
            return errorMessage
        curUser.close()

        # 20
        curViewMemberAndViewArea = self.conn.cursor()
        sqlViewMemberAndViewArea = 'SELECT * FROM view_member_and_view_area WHERE user_id = %s '
        sqlViewMemberAndViewAreaTotalCount = curViewMemberAndViewArea.execute(sqlViewMemberAndViewArea, [user_id])
        if sqlViewMemberAndViewAreaTotalCount:
            is_member = 1
            curViewMemberAndViewAreaResult = curViewMemberAndViewArea.fetchone()
            # print "curViewMemberAndViewAreaResult   ",curViewMemberAndViewAreaResult
            member_name = curViewMemberAndViewAreaResult[1]
            member_email = curViewMemberAndViewAreaResult[2]
            member_province = curViewMemberAndViewAreaResult[3]
            member_city = curViewMemberAndViewAreaResult[4]
            member_district = curViewMemberAndViewAreaResult[5]
            member_address = curViewMemberAndViewAreaResult[6]
        else:
            is_member = 0
            member_name = ""
            member_email = ""
            member_province = ""
            member_city = ""
            member_district = ""
            member_address = ""

        curViewMemberAndViewArea.close()
        # 27
        curItem = self.conn.cursor()
        curItemSQL = "SELECT item_title,item_content,item_url,item_address,item_publishdate, item_deadline,item_handlername,item_handlerphone,dept_id,service_id FROM zzh_item WHERE item_id=%s"
        curItemSQLTotalCount = curItem.execute(curItemSQL, [item_id])
        if curItemSQLTotalCount:
            curItemSQLResult = curItem.fetchone()
            # print "curItemSQLResult   ", curItemSQLResult
            item_name = curItemSQLResult[0]
            item_content = curItemSQLResult[1]
            item_url = curItemSQLResult[2]
            item_address = curItemSQLResult[3]
            item_publishdate = curItemSQLResult[4]
            item_deadline = curItemSQLResult[5]
            item_handlername = curItemSQLResult[6]
            item_handlerphone = curItemSQLResult[7]

            dept_id = curItemSQLResult[8]

            service_id = curItemSQLResult[9]
        else:
            item_name = ""
            item_content = ""
            item_url = ""
            item_address = ""
            item_publishdate = ""
            item_deadline = ""
            item_handlername = ""
            item_handlerphone = ""
            dept_id = 0
            service_id = 0
            errorMessage = "没有符合itemId的记录，请检查item是否存在"
            return errorMessage
        curItem.close()
        # 37
        curService = self.conn.cursor()
        curServiceSQL = "SELECT service_name,service_code,service_price,service_rate,service_content,service_contract FROM zzh_service WHERE service_id=%s "
        curServiceSQLTotalCount = curService.execute(curServiceSQL, [service_id])
        if curServiceSQLTotalCount:
            curServiceSQLResult = curService.fetchone()
            # print "curServiceSQLResult   ", curServiceSQLResult


            service_name = curServiceSQLResult[0]
            service_code = curServiceSQLResult[1]
            service_price = curServiceSQLResult[2]

            service_rate = curServiceSQLResult[3]
            service_content = curServiceSQLResult[4]
            service_contract = curServiceSQLResult[5]

        else:
            service_name = ""
            service_code = ""
            service_price = 0

            service_rate = 0
            service_content = ""
            service_contract = ""
            errorMessage = "没有符合itemId绑定的serviceId的记录，请检查serviceId是否存在"
            return errorMessage
        curService.close()
        # 43
        if is_coupon:
            curCoupon = self.conn.cursor()
            curCouponSQL = "SELECT coupon_name,coupon_code,coupon_type,coupon_conditions,coupon_discount FROM zzh_coupon WHERE coupon_id = %s"
            curCouponSQLTotalCount = curCoupon.execute(curCouponSQL, [coupon_id])
            if curCouponSQLTotalCount:
                curCouponSQLResult = curCoupon.fetchone()
                # print "curCouponSQLResult   ", curCouponSQLResult
                coupon_name = curCouponSQLResult[0]
                coupon_code = curCouponSQLResult[1]
                coupon_type = curCouponSQLResult[2]
                coupon_conditions = curCouponSQLResult[3]
                coupon_discount = curCouponSQLResult[4]
                curCoupon.close()
            else:
                curCoupon.close()
                errorMessage = "没有符合coupon_id的优惠券，请检查coupon_id是否存在"
                return errorMessage
        else:
            coupon_name = ""
            coupon_code = ""
            coupon_type = 1
            coupon_conditions = 1
            coupon_discount = 1


            # 48
        # 获取到zzh_config表游标，读取后台管理员设定的发票比例invoice_rate
        curZZHConfig = self.conn.cursor()
        curZZHConfigSQL = "SELECT invoice_rate FROM zzh_config WHERE id=1"
        curZZHConfigTotalCount = curZZHConfig.execute(curZZHConfigSQL)
        curZZHConfigResult = curZZHConfig.fetchone()
        # print "curZZHConfigResult   ", curZZHConfigResult
        invoice_rate = curZZHConfigResult[0]
        curZZHConfig.close()

        is_invoice = Info[
            'is_invoice']  # 前台传递    是否索要发票，如果这个为1 ，则在zzh_invoice中插入记录。发票等到最后来处理，只有进行支付之后，才会写入zzh_invoice表，并开具发票
        if is_invoice:
            invoice_title = Info['invoice_title']
            invoice_taxes = invoice_rate * order_amount
        else:
            invoice_title = Info['invoice_title']
            invoice_taxes = 0

            # 50

        sql = 'INSERT INTO zzh_order (order_no,order_ip,order_type,trade_no,user_id,' \
              'user_name,payment_id,payment_fee,payment_status,    ' \
              ' order_created_time,is_invoice,invoice_title,invoice_taxes,  ' \
              ' payable_amount,real_amount,order_amount,order_point,is_member,   ' \
              ' member_name,member_email,member_telephone,member_province, member_city,  ' \
              '   member_district,member_address,is_coupon,coupon_name, coupon_code,  ' \
              '   coupon_type,coupon_conditions,coupon_discount,service_content, service_contract,   ' \
              '  service_code,service_name,service_price,service_rate, item_name,  ' \
              ' item_content,item_url,item_address,item_publishdate,item_deadline ,  ' \
              '  item_handlername,item_handlerphone,dept_id,order_status,order_product_is_success,order_message )' \
              ' VALUES ' \
              '  (%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s,' \
              '%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s,%s,' \
              '%s,%s,%s,%s ,%s)'
        cur = self.conn.cursor()
        rs = cur.execute(sql, [order_no, order_ip, order_type, trade_no,
                               user_id, user_name, payment_id, payment_fee, payment_status,
                               order_created_time, is_invoice, invoice_title, invoice_taxes,
                               payable_amount, real_amount, order_amount, order_point, is_member,
                               member_name, member_email, member_telephone, member_province, member_city,
                               member_district, member_address, is_coupon, coupon_name, coupon_code,
                               coupon_type, coupon_conditions, coupon_discount, service_content, service_contract,
                               service_code, service_name, service_price, service_rate, item_name,
                               item_content, item_url, item_address, item_publishdate, item_deadline,
                               item_handlername, item_handlerphone, dept_id, order_status, order_product_is_success,
                               order_message
                               ])
        self.conn.commit()

        sql2 = 'SELECT max(order_id) FROM zzh_order'
        rs2TC = cur.execute(sql2)
        rs2 = cur.fetchone()
        rs2Int = rs2[0]

        sql3 = "SELECT * FROM zzh_order WHERE order_id=%s"
        cur = self.conn.cursor()
        total_count = cur.execute(sql3, [rs2Int])
        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            userOauthJson = Table2JsonByYM.order_list_to_json(result)
            return total_count, userOauthJson
        else:
            cur.close()
            self.conn.close()
            return 0, None

        cur.close()
        self.conn.close()
        if rs:
            return rs,userOauthJson
        else:
            return 0,None

            # 增加了会员功能的插入order表的操作

            # lym insertOrder

    def insertOrder(self, Info, order_no):
        # 01-04

        # order_no = Info['order_no']    #前台传递
        order_type = Info['order_type']  # 前台传递 ,1会员订单 0项目订单 int型
        payable_amount = Info['payable_amount']  # 前台传递
        real_amount = Info['real_amount']  # 前台传递
        order_amount = Info['order_amount']  # 前台传递
        order_point = Info['order_point']
        is_invoice = Info['is_invoice']
        invoice_title = Info['invoice_title']
        payment_id = Info['payment_id']  # 写入支付zzh_payment，绑定order_id和payment_id
        is_coupon = Info['is_coupon']
        coupon_id = Info['coupon_id']  # 前台传递,根据这个读取zzh_coupon的信息
        user_id = Info['user_id']  # 根据这个读取zzh_user和zzh_member
        item_id = Info['item_id']  # item_id   绑定item
        item_name = Info['item_name']


        order_ip = Info['order_ip']
        trade_no = Info['trade_no']
        payment_fee = Info['payment_fee']
        payment_status = Info['payment_status']
        order_status = Info['order_status']
        order_product_is_success = Info['order_product_is_success']
        order_message = Info['order_message']

        payment_time = Info['payment_time']
        order_created_time = localTime  # 当前时间，订单创建时间


        curUser = self.conn.cursor()
        sqlUser = 'SELECT user_name,user_phone FROM zzh_user WHERE user_id=%s'
        curUserTotalCount = curUser.execute(sqlUser, [user_id])
        if curUserTotalCount:
            curuserNameResult = curUser.fetchone()

            user_name = curuserNameResult[0]  # 获取到用户名
            member_telephone = curuserNameResult[1]
        else:
            user_name = ""
            member_telephone = ""
            errorMessage = "没有符合userId的记录，请检查是否注册"
            return 0,errorMessage
        curUser.close()

        #会员订单LYM，member相关字段插入order表
        if order_type == 1:
            curViewMemberAndViewArea = self.conn.cursor()
            sqlViewMemberAndViewArea = 'SELECT member_id,member_name,member_email,province_id,city_id,district_id,member_address  FROM zzh_member WHERE user_id = %s '
            sqlViewMemberAndViewAreaTotalCount = curViewMemberAndViewArea.execute(sqlViewMemberAndViewArea, [user_id])
            if sqlViewMemberAndViewAreaTotalCount:
                is_member = 1
                curViewMemberAndViewAreaResult = curViewMemberAndViewArea.fetchone()
                member_name = curViewMemberAndViewAreaResult[1]
                member_email = curViewMemberAndViewAreaResult[2]
                member_province = curViewMemberAndViewAreaResult[3]
                member_city = curViewMemberAndViewAreaResult[4]
                member_district = curViewMemberAndViewAreaResult[5]
                member_address = curViewMemberAndViewAreaResult[6]
            else:
                is_member = 0
                member_name = ""
                member_email = ""
                member_province = ""
                member_city = ""
                member_district = ""
                member_address = ""

            curViewMemberAndViewArea.close()

            if is_coupon:
                curCoupon = self.conn.cursor()
                curCouponSQL = "SELECT coupon_name,coupon_code,coupon_type,coupon_conditions,coupon_discount FROM zzh_coupon WHERE coupon_id = %s"
                curCouponSQLTotalCount = curCoupon.execute(curCouponSQL, [coupon_id])
                if curCouponSQLTotalCount:
                    curCouponSQLResult = curCoupon.fetchone()
                    # print "curCouponSQLResult   ", curCouponSQLResult
                    coupon_name = curCouponSQLResult[0]
                    coupon_code = curCouponSQLResult[1]
                    coupon_type = curCouponSQLResult[2]
                    coupon_conditions = curCouponSQLResult[3]
                    coupon_discount = curCouponSQLResult[4]
                    curCoupon.close()
                else:
                    curCoupon.close()
                    errorMessage = "没有符合coupon_id的优惠券，请检查coupon_id是否存在"
                    return 0, errorMessage
            else:
                coupon_name = ""
                coupon_code = ""
                coupon_type = 1
                coupon_conditions = 1
                coupon_discount = 1


                # 48
            # 获取到zzh_config表游标，读取后台管理员设定的发票比例invoice_rate
            curZZHConfig = self.conn.cursor()
            curZZHConfigSQL = "SELECT invoice_rate FROM zzh_config WHERE id=1"
            curZZHConfigTotalCount = curZZHConfig.execute(curZZHConfigSQL)
            curZZHConfigResult = curZZHConfig.fetchone()
            # print "curZZHConfigResult   ", curZZHConfigResult
            invoice_rate = float(curZZHConfigResult[0])
            curZZHConfig.close()

            is_invoice = Info['is_invoice']  # 前台传递    是否索要发票，如果这个为1 ，则在zzh_invoice中插入记录。发票等到最后来处理，只有进行支付之后，才会写入zzh_invoice表，并开具发票
            if is_invoice:
                invoice_title = Info['invoice_title']
                invoice_taxes = invoice_rate * float(order_amount)
            else:
                invoice_title = Info['invoice_title']
                invoice_taxes = 0

                # 50

            sql = 'INSERT INTO zzh_order (order_no, order_ip, order_type, trade_no,' \
                  'user_id, user_name, payment_id, payment_fee, ' \
                  'payment_status, order_created_time, is_invoice, invoice_title, ' \
                  'invoice_taxes, payable_amount, real_amount, order_amount, ' \
                  'order_point, is_member,member_name, member_email, ' \
                  'member_telephone, member_province, member_city,member_district, ' \
                  'member_address, is_coupon, coupon_name, coupon_code,' \
                  'coupon_type,coupon_conditions, coupon_discount,order_status, ' \
                  'order_product_is_success,order_message, item_id, item_name )' \
                  ' VALUES ' \
                  '  (%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s ,%s, %s)'
            cur = self.conn.cursor()
            rs = cur.execute(sql, [order_no, order_ip, order_type, trade_no,
                                   user_id, user_name, payment_id, payment_fee,
                                   payment_status, order_created_time, is_invoice, invoice_title,
                                   invoice_taxes, payable_amount, real_amount, order_amount,
                                   order_point, is_member,member_name, member_email,
                                   member_telephone, member_province, member_city,member_district,
                                   member_address, is_coupon, coupon_name, coupon_code,
                                   coupon_type,coupon_conditions, coupon_discount,order_status,
                                   order_product_is_success,order_message, item_id, item_name
                                   ])
            self.conn.commit()

            sql2 = 'SELECT max(order_id) FROM zzh_order'
            rs2TC = cur.execute(sql2)
            rs2 = cur.fetchone()
            rs2Int = rs2[0]

            sql3 = "SELECT * FROM zzh_order WHERE order_id=%s"
            cur = self.conn.cursor()
            total_count = cur.execute(sql3, [rs2Int])
            if total_count:

                result = cur.fetchall()
                cur.close()
                self.conn.close()
                userOauthJson = Table2JsonByYM.order_list_to_json(result)

                return (total_count, userOauthJson)
            else:
                cur.close()
                self.conn.close()

                return (0, None)




        #非会员订单,member表 item表相关的字段插入order表
        if order_type == 2:
            # 20

            curViewMemberAndViewArea = self.conn.cursor()
            sqlViewMemberAndViewArea = 'SELECT member_id,member_name,member_email,province_id,city_id,district_id,member_address  FROM zzh_member WHERE user_id = %s '
            sqlViewMemberAndViewAreaTotalCount = curViewMemberAndViewArea.execute(sqlViewMemberAndViewArea, [user_id])
            if sqlViewMemberAndViewAreaTotalCount:
                is_member = 1
                curViewMemberAndViewAreaResult = curViewMemberAndViewArea.fetchone()
                member_name = curViewMemberAndViewAreaResult[1]
                member_email = curViewMemberAndViewAreaResult[2]
                member_province = curViewMemberAndViewAreaResult[3]
                member_city = curViewMemberAndViewAreaResult[4]
                member_district = curViewMemberAndViewAreaResult[5]
                member_address = curViewMemberAndViewAreaResult[6]
            else:
                is_member = 0
                member_name = ""
                member_email = ""
                member_province = ""
                member_city = ""
                member_district = ""
                member_address = ""

            curViewMemberAndViewArea.close()
            # 27
            curItem = self.conn.cursor()
            curItemSQL = "SELECT item_title,item_content,item_url,item_address,item_publishdate, item_deadline,item_handlername,item_handlerphone,dept_id,service_id FROM zzh_item WHERE item_id=%s"
            curItemSQLTotalCount = curItem.execute(curItemSQL, [item_id])
            if curItemSQLTotalCount:
                curItemSQLResult = curItem.fetchone()
                # print "curItemSQLResult   ", curItemSQLResult
                item_name = curItemSQLResult[0]
                item_content = curItemSQLResult[1]
                item_url = curItemSQLResult[2]
                item_address = curItemSQLResult[3]
                item_publishdate = curItemSQLResult[4]
                item_deadline = curItemSQLResult[5]
                item_handlername = curItemSQLResult[6]
                item_handlerphone = curItemSQLResult[7]

                dept_id = curItemSQLResult[8]

                service_id = curItemSQLResult[9]
            else:
                item_name = ""
                item_content = ""
                item_url = ""
                item_address = ""
                item_publishdate = ""
                item_deadline = ""
                item_handlername = ""
                item_handlerphone = ""
                dept_id = 0
                service_id = 0
                errorMessage = "没有符合itemId的记录，请检查item是否存在"
                return 0, errorMessage
            curItem.close()
            # 37
            curService = self.conn.cursor()
            curServiceSQL = "SELECT service_name,service_code,service_price,service_rate,service_content,service_contract FROM zzh_service WHERE service_id=%s "
            curServiceSQLTotalCount = curService.execute(curServiceSQL, [service_id])
            if curServiceSQLTotalCount:
                curServiceSQLResult = curService.fetchone()
                # print "curServiceSQLResult   ", curServiceSQLResult


                service_name = curServiceSQLResult[0]
                service_code = curServiceSQLResult[1]
                service_price = curServiceSQLResult[2]

                service_rate = curServiceSQLResult[3]
                service_content = curServiceSQLResult[4]
                service_contract = curServiceSQLResult[5]

            else:
                service_name = ""
                service_code = ""
                service_price = 0

                service_rate = 0
                service_content = ""
                service_contract = ""
                errorMessage = "没有符合itemId绑定的serviceId的记录，请检查serviceId是否存在"
                return 0, errorMessage
            curService.close()
            # 43
            if is_coupon:
                curCoupon = self.conn.cursor()
                curCouponSQL = "SELECT coupon_name,coupon_code,coupon_type,coupon_conditions,coupon_discount FROM zzh_coupon WHERE coupon_id = %s"
                curCouponSQLTotalCount = curCoupon.execute(curCouponSQL, [coupon_id])
                if curCouponSQLTotalCount:
                    curCouponSQLResult = curCoupon.fetchone()
                    # print "curCouponSQLResult   ", curCouponSQLResult
                    coupon_name = curCouponSQLResult[0]
                    coupon_code = curCouponSQLResult[1]
                    coupon_type = curCouponSQLResult[2]
                    coupon_conditions = curCouponSQLResult[3]
                    coupon_discount = curCouponSQLResult[4]
                    curCoupon.close()
                else:
                    curCoupon.close()
                    errorMessage = "没有符合coupon_id的优惠券，请检查coupon_id是否存在"
                    return 0, errorMessage
            else:
                coupon_name = ""
                coupon_code = ""
                coupon_type = 1
                coupon_conditions = 1
                coupon_discount = 1


                # 48
            # 获取到zzh_config表游标，读取后台管理员设定的发票比例invoice_rate
            curZZHConfig = self.conn.cursor()
            curZZHConfigSQL = "SELECT invoice_rate FROM zzh_config WHERE id=1"
            curZZHConfigTotalCount = curZZHConfig.execute(curZZHConfigSQL)
            curZZHConfigResult = curZZHConfig.fetchone()
            # print "curZZHConfigResult   ", curZZHConfigResult
            invoice_rate = float(curZZHConfigResult[0])
            curZZHConfig.close()

            is_invoice = Info[
                'is_invoice']  # 前台传递    是否索要发票，如果这个为1 ，则在zzh_invoice中插入记录。发票等到最后来处理，只有进行支付之后，才会写入zzh_invoice表，并开具发票
            if is_invoice:
                invoice_title = Info['invoice_title']
                invoice_taxes = invoice_rate * float(order_amount)
            else:
                invoice_title = Info['invoice_title']
                invoice_taxes = 0

                # 50

            sql = 'INSERT INTO zzh_order (order_no,order_ip,order_type,trade_no,user_id,' \
                  'user_name,payment_id,payment_fee,payment_status,    ' \
                  ' order_created_time,is_invoice,invoice_title,invoice_taxes,  ' \
                  ' payable_amount,real_amount,order_amount,order_point,is_member,   ' \
                  ' member_name,member_email,member_telephone,member_province, member_city,  ' \
                  '   member_district,member_address,is_coupon,coupon_name, coupon_code,  ' \
                  '   coupon_type,coupon_conditions,coupon_discount,service_content, service_contract,   ' \
                  '  service_code,service_name,service_price,service_rate, item_name,  ' \
                  ' item_content,item_url,item_address,item_publishdate,item_deadline ,  ' \
                  '  item_handlername,item_handlerphone,dept_id,order_status,order_product_is_success,order_message, item_id )' \
                  ' VALUES ' \
                  '  (%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s,%s,' \
                  '%s,%s,%s,%s ,%s, %s)'
            cur = self.conn.cursor()
            rs = cur.execute(sql, [order_no, order_ip, order_type, trade_no,
                                   user_id, user_name, payment_id, payment_fee, payment_status,
                                   order_created_time, is_invoice, invoice_title, invoice_taxes,
                                   payable_amount, real_amount, order_amount, order_point, is_member,
                                   member_name, member_email, member_telephone, member_province, member_city,
                                   member_district, member_address, is_coupon, coupon_name, coupon_code,
                                   coupon_type, coupon_conditions, coupon_discount, service_content, service_contract,
                                   service_code, service_name, service_price, service_rate, item_name,
                                   item_content, item_url, item_address, item_publishdate, item_deadline,
                                   item_handlername, item_handlerphone, dept_id, order_status, order_product_is_success,
                                   order_message, item_id
                                   ])
            self.conn.commit()

            sql2 = 'SELECT max(order_id) FROM zzh_order'
            rs2TC = cur.execute(sql2)
            rs2 = cur.fetchone()
            rs2Int = rs2[0]

            sql3 = "SELECT * FROM zzh_order WHERE order_id=%s"
            cur = self.conn.cursor()
            total_count = cur.execute(sql3, [rs2Int])
            if total_count:
                result = cur.fetchall()
                cur.close()
                self.conn.close()
                userOauthJson = Table2JsonByYM.order_list_to_json(result)

                return (total_count, userOauthJson)

            else:
                cur.close()
                self.conn.close()
                return (0, None)

            # cur.close()
            # self.conn.close()

    def selectOrder(self, order_id):
        """
        根据ID查询信息，返回元组

        """
        sql = "select * from zzh_order WHERE order_id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [order_id])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr

    def updateOrder(self, Info):  #

        order_id = Info['order_id']  # 必填，请传递
        # 01-04
        order_no = Info['order_no']
        order_ip = Info['order_ip']  #
        order_type = Info['order_type']
        trade_no = Info['trade_no']

        # 05-09
        user_id = Info['user_id'] #
        user_name = Info['user_name']
        payment_id = Info['payment_id']  #
        payment_fee = Info['payment_fee']
        payment_status = Info['payment_status']

        # 10-14
        payment_time = Info['payment_time']  #
        order_created_time = localTime
        is_invoice = Info['is_invoice']  #
        invoice_title = Info['invoice_title']
        invoice_taxes = Info['invoice_taxes']

        # 15-19
        payable_amount = Info['payable_amount']  #
        real_amount = Info['real_amount']
        order_amount = Info['order_amount']  #
        order_point = Info['order_point']
        is_member = Info['is_member']

        # 20-24
        member_name = Info['member_name']  #
        member_email = Info['member_email']
        member_telephone = Info['member_telephone']  #
        member_province = Info['member_province']
        member_city = Info['member_city']

        # 25-29
        member_district = Info['member_district']  #
        member_address = Info['member_address']
        is_coupon = Info['is_coupon']  #
        coupon_name = Info['coupon_name']
        coupon_code = Info['coupon_code']

        # 30-34
        coupon_type = Info['coupon_type']  #
        coupon_conditions = Info['coupon_conditions']
        coupon_discount = Info['coupon_discount']  #
        service_content = Info['service_content']
        service_contract = Info['service_contract']

        # 35-39
        service_code = Info['service_code']  #
        service_name = Info['service_name']
        service_price = Info['service_price']  #
        service_rate = Info['service_rate']
        item_name = Info['item_name']

        # 40-44
        item_content = Info['item_content']  #
        item_url = Info['item_url']
        item_address = Info['item_address']  #
        item_publishdate = Info['item_publishdate']
        item_deadline = Info['item_deadline']

        # 45-49
        item_handlername = Info['item_handlername']  #
        item_handlerphone = Info['item_handlerphone']
        dept_id = Info['dept_id']  #
        order_status = Info['order_status']
        order_product_is_success = Info['order_product_is_success']
        order_message = Info['order_message']
        item_id = Info['item_id']

        coupon_id = Info['coupon_id']
        #根据user_id查出user_name和member_telephone
        curUser = self.conn.cursor()
        sqlUser = 'SELECT user_name,user_phone FROM zzh_user WHERE user_id=%s'
        curUserTotalCount = curUser.execute(sqlUser, [user_id])

        if curUserTotalCount:
            curuserNameResult = curUser.fetchone()

            user_name = curuserNameResult[0]  # 获取到用户名
            member_telephone = curuserNameResult[1]
        else:
            user_name = ""
            member_telephone = ""
            errorMessage = "没有符合userId的记录，请检查是否注册"
            return 0, errorMessage
        curUser.close()


        #如果是会员订单，需要更新orde表r,member表

        if order_type == 1:


            curViewMemberAndViewArea = self.conn.cursor()
            sqlViewMemberAndViewArea = 'SELECT member_id,member_name,member_email,province_id,city_id,district_id,member_address  FROM zzh_member WHERE user_id = %s '
            sqlViewMemberAndViewAreaTotalCount = curViewMemberAndViewArea.execute(sqlViewMemberAndViewArea, [user_id])
            if sqlViewMemberAndViewAreaTotalCount:
                is_member = 1
                curViewMemberAndViewAreaResult = curViewMemberAndViewArea.fetchone()
                member_name = curViewMemberAndViewAreaResult[1]
                member_email = curViewMemberAndViewAreaResult[2]
                member_province = curViewMemberAndViewAreaResult[3]
                member_city = curViewMemberAndViewAreaResult[4]
                member_district = curViewMemberAndViewAreaResult[5]
                member_address = curViewMemberAndViewAreaResult[6]
            else:
                is_member = 0
                member_name = ""
                member_email = ""
                member_province = ""
                member_city = ""
                member_district = ""
                member_address = ""

            curViewMemberAndViewArea.close()

            if is_coupon:
                curCoupon = self.conn.cursor()
                curCouponSQL = "SELECT coupon_name,coupon_code,coupon_type,coupon_conditions,coupon_discount FROM zzh_coupon WHERE coupon_id = %s"
                curCouponSQLTotalCount = curCoupon.execute(curCouponSQL, [coupon_id])
                if curCouponSQLTotalCount:
                    curCouponSQLResult = curCoupon.fetchone()
                    # print "curCouponSQLResult   ", curCouponSQLResult
                    coupon_name = curCouponSQLResult[0]
                    coupon_code = curCouponSQLResult[1]
                    coupon_type = curCouponSQLResult[2]
                    coupon_conditions = curCouponSQLResult[3]
                    coupon_discount = curCouponSQLResult[4]
                    curCoupon.close()
                else:
                    curCoupon.close()
                    errorMessage = "没有符合coupon_id的优惠券，请检查coupon_id是否存在"
                    return 0, errorMessage
            else:
                coupon_name = ""
                coupon_code = ""
                coupon_type = 1
                coupon_conditions = 1
                coupon_discount = 1


                # 48
            # 获取到zzh_config表游标，读取后台管理员设定的发票比例invoice_rate
            curZZHConfig = self.conn.cursor()
            curZZHConfigSQL = "SELECT invoice_rate FROM zzh_config WHERE id=1"
            curZZHConfigTotalCount = curZZHConfig.execute(curZZHConfigSQL)
            curZZHConfigResult = curZZHConfig.fetchone()
            # print "curZZHConfigResult   ", curZZHConfigResult
            invoice_rate = float(curZZHConfigResult[0])
            curZZHConfig.close()

            is_invoice = Info['is_invoice']  # 前台传递    是否索要发票，如果这个为1 ，则在zzh_invoice中插入记录。发票等到最后来处理，只有进行支付之后，才会写入zzh_invoice表，并开具发票
            if is_invoice:
                invoice_title = Info['invoice_title']
                invoice_taxes = invoice_rate * float(order_amount)
            else:
                invoice_title = Info['invoice_title']
                invoice_taxes = 0

            '''
            #1.更新member表
            curUpdateMember = self.conn.cursor()

            sqlMember = 'UPDATE zzh_member SET is_member=%s, member_name=%s, member_email=%s,member_telephone=%s,member_provinece=%s,member_city=%s,member_district=%s,member_address=%s WHERE member_id='


            rsMember = curUpdateMember.execute(sqlMember,[])


            curUpdateMember.close()
            '''

            #2.更新order表
            curUpdateOrder = self.conn.cursor()
            sqlOrder = 'UPDATE zzh_order SET ' \
                  '             order_no=%s,order_ip=%s,order_type=%s,trade_no=%s, ' \
     \
                  'user_id=%s,user_name =%s, payment_id =%s , payment_fee =%s  ,payment_status=%s,' \
     \
                  '      payment_time=%s,order_created_time=%s,is_invoice=%s,invoice_title=%s,invoice_taxes=%s,' \
     \
                  '    payable_amount=%s,real_amount=%s,order_amount=%s,order_point=%s,is_member=%s,   ' \
     \
                  '     member_name=%s,member_email=%s,member_telephone=%s,member_province=%s, member_city=%s,  ' \
     \
                  '   member_district=%s,member_address=%s,is_coupon=%s,coupon_name=%s, coupon_code=%s,  ' \
     \
                  '   coupon_type=%s,coupon_conditions=%s,coupon_discount=%s,service_content=%s, service_contract=%s,  ' \
     \
                  '   service_code=%s,service_name=%s,service_price=%s,service_rate=%s, item_name=%s,  ' \
     \
                  '   item_content=%s,item_url=%s,item_address=%s,item_publishdate=%s,item_deadline=%s , ' \
     \
                  '   item_handlername=%s,item_handlerphone=%s,dept_id=%s,order_status=%s,order_product_is_success=%s,order_message=%s, item_id=%s ' \
                  'WHERE order_id=%s'

            rsOrder = curUpdateOrder.execute(sqlOrder, [order_no, order_ip, order_type, trade_no,
                                   user_id, user_name, payment_id, payment_fee, payment_status,
                                   payment_time, order_created_time, is_invoice, invoice_title, invoice_taxes,
                                   payable_amount, real_amount, order_amount, order_point, is_member,
                                   member_name, member_email, member_telephone, member_province, member_city,
                                   member_district, member_address, is_coupon, coupon_name, coupon_code,
                                   coupon_type, coupon_conditions, coupon_discount, service_content, service_contract,
                                   service_code, service_name, service_price, service_rate, item_name,
                                   item_content, item_url, item_address, item_publishdate, item_deadline,
                                   item_handlername, item_handlerphone, dept_id, order_status, order_product_is_success,
                                   order_message,item_id,
                                   order_id])

            curUpdateOrder.close()

        #如果是项目订单，根据couponId，userId，itemId，查询coupon基表，查询menber基表，查询item基表，修改order表内容
        if order_type == 2:
            '''
            #1. 根据couponId更新coupon表,暂时不需要更新
            curUpdateCoupon = self.conn.cursor()
            sqlCoupon = ''
            rsCoupon = curUpdateCoupon.execute(sqlCoupon,[])
            curUpdateCoupon.close()
            #2. 根据userId更新member表，根据传入的参数暂时这里还不需要更新
            curUpdateMember = self.conn.cursor()
            sqlMember = ''
            rsCoupon = curUpdateCoupon.execute(sqlMember, [])
            curUpdateMember.close()
            #3. 根据itemId更新item表，根据传入的参数暂时这里还不需要更新
            curUpdateItem = self.conn.cursor()
            sqlItem = ''
            rsItem = curUpdateCoupon.execute(sqlMember, [])
            curUpdateItem.close()
            '''
            curViewMemberAndViewArea = self.conn.cursor()
            sqlViewMemberAndViewArea = 'SELECT member_id,member_name,member_email,province_id,city_id,district_id,member_address  FROM zzh_member WHERE user_id = %s '
            sqlViewMemberAndViewAreaTotalCount = curViewMemberAndViewArea.execute(sqlViewMemberAndViewArea, [user_id])
            if sqlViewMemberAndViewAreaTotalCount:
                is_member = 1
                curViewMemberAndViewAreaResult = curViewMemberAndViewArea.fetchone()
                member_name = curViewMemberAndViewAreaResult[1]
                member_email = curViewMemberAndViewAreaResult[2]
                member_province = curViewMemberAndViewAreaResult[3]
                member_city = curViewMemberAndViewAreaResult[4]
                member_district = curViewMemberAndViewAreaResult[5]
                member_address = curViewMemberAndViewAreaResult[6]
            else:
                is_member = 0
                member_name = ""
                member_email = ""
                member_province = ""
                member_city = ""
                member_district = ""
                member_address = ""

            curViewMemberAndViewArea.close()
            # 27
            curItem = self.conn.cursor()
            curItemSQL = "SELECT item_title,item_content,item_url,item_address,item_publishdate, item_deadline,item_handlername,item_handlerphone,dept_id,service_id FROM zzh_item WHERE item_id=%s"
            curItemSQLTotalCount = curItem.execute(curItemSQL, [item_id])
            if curItemSQLTotalCount:
                curItemSQLResult = curItem.fetchone()
                # print "curItemSQLResult   ", curItemSQLResult
                item_name = curItemSQLResult[0]
                item_content = curItemSQLResult[1]
                item_url = curItemSQLResult[2]
                item_address = curItemSQLResult[3]
                item_publishdate = curItemSQLResult[4]
                item_deadline = curItemSQLResult[5]
                item_handlername = curItemSQLResult[6]
                item_handlerphone = curItemSQLResult[7]

                dept_id = curItemSQLResult[8]

                service_id = curItemSQLResult[9]
            else:
                item_name = ""
                item_content = ""
                item_url = ""
                item_address = ""
                item_publishdate = ""
                item_deadline = ""
                item_handlername = ""
                item_handlerphone = ""
                dept_id = 0
                service_id = 0
                errorMessage = "没有符合itemId的记录，请检查item是否存在"
                return 0, errorMessage
            curItem.close()
            # 37
            curService = self.conn.cursor()
            curServiceSQL = "SELECT service_name,service_code,service_price,service_rate,service_content,service_contract FROM zzh_service WHERE service_id=%s "
            curServiceSQLTotalCount = curService.execute(curServiceSQL, [service_id])
            if curServiceSQLTotalCount:
                curServiceSQLResult = curService.fetchone()
                # print "curServiceSQLResult   ", curServiceSQLResult


                service_name = curServiceSQLResult[0]
                service_code = curServiceSQLResult[1]
                service_price = curServiceSQLResult[2]

                service_rate = curServiceSQLResult[3]
                service_content = curServiceSQLResult[4]
                service_contract = curServiceSQLResult[5]

            else:
                service_name = ""
                service_code = ""
                service_price = 0

                service_rate = 0
                service_content = ""
                service_contract = ""
                errorMessage = "没有符合itemId绑定的serviceId的记录，请检查serviceId是否存在"
                return 0, errorMessage
            curService.close()
            # 43
            if is_coupon:
                curCoupon = self.conn.cursor()
                curCouponSQL = "SELECT coupon_name,coupon_code,coupon_type,coupon_conditions,coupon_discount FROM zzh_coupon WHERE coupon_id = %s"
                curCouponSQLTotalCount = curCoupon.execute(curCouponSQL, [coupon_id])
                if curCouponSQLTotalCount:
                    curCouponSQLResult = curCoupon.fetchone()
                    # print "curCouponSQLResult   ", curCouponSQLResult
                    coupon_name = curCouponSQLResult[0]
                    coupon_code = curCouponSQLResult[1]
                    coupon_type = curCouponSQLResult[2]
                    coupon_conditions = curCouponSQLResult[3]
                    coupon_discount = curCouponSQLResult[4]
                    curCoupon.close()
                else:
                    curCoupon.close()
                    errorMessage = "没有符合coupon_id的优惠券，请检查coupon_id是否存在"
                    return 0, errorMessage
            else:
                coupon_name = ""
                coupon_code = ""
                coupon_type = 1
                coupon_conditions = 1
                coupon_discount = 1


                # 48
            # 获取到zzh_config表游标，读取后台管理员设定的发票比例invoice_rate
            curZZHConfig = self.conn.cursor()
            curZZHConfigSQL = "SELECT invoice_rate FROM zzh_config WHERE id=1"
            curZZHConfigTotalCount = curZZHConfig.execute(curZZHConfigSQL)
            curZZHConfigResult = curZZHConfig.fetchone()
            # print "curZZHConfigResult   ", curZZHConfigResult
            invoice_rate = float(curZZHConfigResult[0])
            curZZHConfig.close()

            is_invoice = Info[
                'is_invoice']  # 前台传递    是否索要发票，如果这个为1 ，则在zzh_invoice中插入记录。发票等到最后来处理，只有进行支付之后，才会写入zzh_invoice表，并开具发票
            if is_invoice:
                invoice_title = Info['invoice_title']
                invoice_taxes = invoice_rate * float(order_amount)
            else:
                invoice_title = Info['invoice_title']
                invoice_taxes = 0

                # 50
            #4. 更新Order表
            curUpdateOrder = self.conn.cursor()
            sqlOrder = 'UPDATE zzh_order SET ' \
                  '             order_no=%s,order_ip=%s,order_type=%s,trade_no=%s, ' \
 \
                  'user_id=%s,user_name =%s, payment_id =%s , payment_fee =%s  ,payment_status=%s,' \
 \
                  '      payment_time=%s,order_created_time=%s,is_invoice=%s,invoice_title=%s,invoice_taxes=%s,' \
 \
                  '    payable_amount=%s,real_amount=%s,order_amount=%s,order_point=%s,is_member=%s,   ' \
 \
                  '     member_name=%s,member_email=%s,member_telephone=%s,member_province=%s, member_city=%s,  ' \
 \
                  '   member_district=%s,member_address=%s,is_coupon=%s,coupon_name=%s, coupon_code=%s,  ' \
 \
                  '   coupon_type=%s,coupon_conditions=%s,coupon_discount=%s,service_content=%s, service_contract=%s,  ' \
 \
                  '   service_code=%s,service_name=%s,service_price=%s,service_rate=%s, item_name=%s,  ' \
 \
                  '   item_content=%s,item_url=%s,item_address=%s,item_publishdate=%s,item_deadline=%s , ' \
 \
                  '   item_handlername=%s,item_handlerphone=%s,dept_id=%s,order_status=%s,order_product_is_success=%s,order_message=%s,item_id=%s ' \
                  'WHERE order_id=%s'
            rsOrder = curUpdateOrder.execute(sqlOrder, [order_no, order_ip, order_type, trade_no,
                                                        user_id, user_name, payment_id, payment_fee, payment_status,
                                                        payment_time, order_created_time, is_invoice, invoice_title,
                                                        invoice_taxes,
                                                        payable_amount, real_amount, order_amount, order_point,
                                                        is_member,
                                                        member_name, member_email, member_telephone, member_province,
                                                        member_city,
                                                        member_district, member_address, is_coupon, coupon_name,
                                                        coupon_code,
                                                        coupon_type, coupon_conditions, coupon_discount,
                                                        service_content, service_contract,
                                                        service_code, service_name, service_price, service_rate,
                                                        item_name,
                                                        item_content, item_url, item_address, item_publishdate,
                                                        item_deadline,
                                                        item_handlername, item_handlerphone, dept_id, order_status,
                                                        order_product_is_success,
                                                        order_message, item_id,
                                                        order_id])
            curUpdateOrder.close()

        self.conn.commit()

        self.conn.close()
        if rsOrder:
            return rsOrder
        else:
            return None

    def deleteOrder(self, ids):
        """
        根据ID删除数据表信息，ID需要以数组形式传递
        :param adminIdArray: id 构成的数组 []
        :return:
        """
        sql = "delete from zzh_order WHERE order_id=%s"
        cur = self.conn.cursor()
        for id in ids:
            rs = cur.execute(sql, [id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def removeOrder(self, ids, userId):
        sql = "UPDATE   zzh_order  SET order_status=-1 WHERE order_id=%s AND user_id=%s"
        cur = self.conn.cursor()
        for id in ids:
            rs = cur.execute(sql, [id, userId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateOrderOld(self,Info):#

        order_id = Info['order_id']  # 必填，请传递
         #01-04
        order_no = Info['order_no']
        order_ip = Info['order_ip']  #
        order_type = Info['order_type']
        trade_no = Info['trade_no']


        #05-09
        user_id = Info['user_id']  #
        user_name = Info['user_name']
        payment_id = Info['payment_id']  #
        payment_fee = Info['payment_fee']
        payment_status = Info['payment_status']

        #10-14
        payment_time = Info['payment_time']  #
        order_created_time = localTime
        is_invoice = Info['is_invoice']  #
        invoice_title = Info['invoice_title']
        invoice_taxes = Info['invoice_taxes']

        #15-19
        payable_amount = Info['payable_amount']  #
        real_amount = Info['real_amount']
        order_amount = Info['order_amount']  #
        order_point = Info['order_point']
        is_member = Info['is_member']

        #20-24
        member_name = Info['member_name']  #
        member_email = Info['member_email']
        member_telephone = Info['member_telephone']  #
        member_province = Info['member_province']
        member_city = Info['member_city']


                #25-29
        member_district = Info['member_district']  #
        member_address = Info['member_address']
        is_coupon = Info['is_coupon']  #
        coupon_name = Info['coupon_name']
        coupon_code = Info['coupon_code']


                #30-34
        coupon_type = Info['coupon_type']  #
        coupon_conditions = Info['coupon_conditions']
        coupon_discount = Info['coupon_discount']  #
        service_content = Info['service_content']
        service_contract = Info['service_contract']

                #35-39
        service_code = Info['service_code']  #
        service_name = Info['service_name']
        service_price = Info['service_price']  #
        service_rate = Info['service_rate']
        item_name = Info['item_name']

                #40-44
        item_content = Info['item_content']  #
        item_url = Info['item_url']
        item_address = Info['item_address']  #
        item_publishdate = Info['item_publishdate']
        item_deadline = Info['item_deadline']

                #45-49
        item_handlername = Info['item_handlername']  #
        item_handlerphone = Info['item_handlerphone']
        dept_id = Info['dept_id']  #
        order_status = Info['order_status']
        order_product_is_success = Info['order_product_is_success']
        order_message = Info['order_message']



        cur = self.conn.cursor()
        sql = 'UPDATE zzh_order SET ' \
              '             order_no=%s,order_ip=%s,order_type=%s,trade_no=%s, ' \
        \
              'user_id=%s,user_name =%s, payment_id =%s , payment_fee =%s  ,payment_status=%s,' \
        \
              '      payment_time=%s,order_created_time=%s,is_invoice=%s,invoice_title=%s,invoice_taxes=%s,' \
        \
              '    payable_amount=%s,real_amount=%s,order_amount=%s,order_point=%s,is_member=%s,   ' \
        \
              '     member_name=%s,member_email=%s,member_telephone=%s,member_province=%s, member_city=%s,  ' \
        \
              '   member_district=%s,member_address=%s,is_coupon=%s,coupon_name=%s, coupon_code=%s,  ' \
            \
              '   coupon_type=%s,coupon_conditions=%s,coupon_discount=%s,service_content=%s, service_contract=%s,  ' \
            \
              '   service_code=%s,service_name=%s,service_price=%s,service_rate=%s, item_name=%s,  ' \
            \
              '   item_content=%s,item_url=%s,item_address=%s,item_publishdate=%s,item_deadline=%s , ' \
            \
              '   item_handlername=%s,item_handlerphone=%s,dept_id=%s,order_status=%s,order_product_is_success=%s,order_message=%s ' \
            'WHERE order_id=%s'

        rs=cur.execute(sql,[order_no,order_ip,order_type,trade_no,
                            user_id,user_name,payment_id,payment_fee,payment_status,
                            payment_time,order_created_time,is_invoice,invoice_title,invoice_taxes,
                            payable_amount,real_amount,order_amount,order_point,is_member,
                            member_name,member_email,member_telephone,member_province, member_city,
                            member_district,member_address,is_coupon,coupon_name, coupon_code,
                            coupon_type,coupon_conditions,coupon_discount,service_content, service_contract,
                            service_code,service_name,service_price,service_rate, item_name,
                            item_content,item_url,item_address,item_publishdate,item_deadline ,
                            item_handlername,item_handlerphone,dept_id,order_status,order_product_is_success,order_message,
                            order_id])

        self.conn.commit()
        cur.close()
        self.conn.close()
        if rs:
            return rs
        else:
            return None




    def deleteOrder(self, ids):
        """
        根据ID删除数据表信息，ID需要以数组形式传递
        :param adminIdArray: id 构成的数组 []
        :return:
        """
        sql = "delete from zzh_order WHERE order_id=%s"
        cur = self.conn.cursor()
        for id in ids:
            rs = cur.execute(sql, [id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def removeOrder(self, ids, userId):
        sql = "UPDATE   zzh_order  SET order_status=-1 WHERE order_id=%s AND user_id=%s"
        cur = self.conn.cursor()
        for id in ids:
            rs = cur.execute(sql, [id, userId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs
